intermediate/OrderedCollection

From ggc

Jump to: navigation, search

01 package intermediate;
02 //start auto-imports
03 import java.awt.*;
04 import java.util.*;
05 //end auto-imports
06 
07 
08 /**
09  * All about my application.
10  @author My Name Here
11  */
12 public class OrderedCollection<T extends Comparable, V>
13 {
14   private ArrayList<T> collect;
15   private ArrayList<V> values;
16 
17   public OrderedCollection()
18   {
19     collect=new ArrayList<T>();
20     values=new ArrayList<V>();
21   }
22 
23   public void insert(T t, V v)
24   {
25     for(int i=0; i<collect.size(); i++)
26     {
27       if(collect.get(i).compareTo(t)>0)
28       {
29         collect.add(i, t);
30         values.add(i, v);
31         return;
32       }
33     }
34     values.add(v);
35     collect.add(t);
36   }
37 
38   public String toString()
39   {
40     String output="[";
41     for(int i=0; i<collect.size(); i++)
42     {
43       output+="("+collect.get(i).toString()+", "+values.get(i).toString()+"), ";
44     }
45     output+="]";
46     return output;
47   }
48 
49   public static void main(String[] args)
50   {
51     OrderedCollection<StringInteger> ordered=new OrderedCollection<StringInteger>();
52     ordered.insert("Howdy"56);
53     ordered.insert("Apples"76);
54     ordered.insert("Doody"96);
55     System.out.println(ordered);
56   }
57 }


Download/View intermediate/OrderedCollection.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter