Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: ArrayList question

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    29
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default ArrayList question

    Hi,

    How do you create ArrayList for individual items like this:

    Object str = "A"
    ArrayList<Objects> str = new ArrayList<Objects>();

    I'm making my own Hashmap but I don't know how to create individual ArrayList with varied names.
    eg) ArrayList<String> Keys = new ArrayList<String>();
    for(int i = 0; i < n) Keys.add(i); #pseudo

    for (int i =0; i <n)
    ArrayList<String> Keys.get(i) = new ArrayList<String>(); #1 would have its own arrayList. 2 would have its own arraylist etc.

    Thanks


  2. #2

    Default Re: ArrayList question

    In order to create your own hashmap, you need a key->value pair.

    This would be helpful:
    public class HashMapImp
    {
    	public static void main(String args[])
    	{
    		MyHashMap mhm = new MyHashMap();
    		mhm.addPair("twenty","20");
    		System.out.println(mhm.getValue("twenty"));
    	}
     
    	class MyHashMap
    	{
    		java.util.ArrayList<KeyValPair> hmap;
     
    		public MyHashMap()
    		{
    			this.hmap = new java.util.ArrayList<KeyValPair>();
    		}
     
    		public boolean addPair(String key, String value)
    		{
    			return this.hmap.add(new KeyValPair(key, value);
    		}
     
    		public String getValue(String key)
    		{
    			for(int i=0; i<this.hmap.size(); i++)
    			{
    				if(this.hmap.get(i).getKey().equals(key))
    				{
    					return this.hmap.get(i).getValue();
    				}
    			}
    			return null;
    		}
     
    		public boolean setValue(String key, String value)
    		{
    			for(int i=0; i<this.hmap.size(); i++)
    			{
    				if(this.hmap.get(i).getKey().equals(key))
    				{
    					this.hmap.get(i).setValue(value);
    					return true;
    				}
    			}
    			return addPair(key,value);
    		}
    	}
     
    	class KeyValPair
    	{
    		private String key;
    		private String value;
     
    		public KeyValPair(String key, String value)
    		{
    			setKey(key);
    			setValue(value);
    		}
     
    		public KeyValPair()
    		{
    			this("","");
    		}
     
    		public void setKey(String key)
    		{
    			this.key = key;
    		}
     
    		public void setValue(String value)
    		{
    			this.value = value;
    		}
     
    		public String getKey()
    		{
    			return this.key;
    		}
     
    		public String getValue()
    		{
    			return this.value;
    		}
    	}
    }
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  3. The Following User Says Thank You to kenster421 For This Useful Post:

    steel55677 (February 23rd, 2012)

  4. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    29
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList question

    Thanks, I tried to do it last night. This is my map currently.
    public class OwnMap<Key,Value>{
     
    		static private Object Key, Value;
     
    		public OwnMap(Object Key, Object Value){
     
    			this.Key = Key;
    			this.Value = Value;
    		}	
     
    		public static Object getKey(){
                  return Key;
    		}
     
    		public static Object getValues(){
    			return Value;
    		}
     
     
    		public static void main(String args[]){
     
    			OwnMap m = new OwnMap("Hello", "123456");
     
    			System.out.println(m.getKey());
    			System.out.print(m.getValues());	
     
    		}
     
    	}
    I'm just not able to create an ArrayList of an ArrayList which is what a hashmap is.
    eg) I have Keys (A,B,C,D,E). They will be added to an Arraylist called Keys. I also want to create an ArrayList of the elements in the Key's ArrayList.
    eg) A = new ArrayList();, B = new ArrayList(); and they will contain the values.

    I just have trouble with that part =\

  5. #4
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: ArrayList question

    This may be over simplified, but I don't quite understand what you want if not this.

     
    ArrayList() keys = new ArrayList();
    ArrayList() a = new ArrayList();
    ArrayList() b = new ArrayList();
    ArrayList() c = new ArrayList();
    ArrayList() d = new ArrayList();
    ArrayList() e = new ArrayList();
     
    keys.add(a);
    keys.add(b);
    keys.add(c);
    keys.add(d);
    keys.add(e);

  6. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    29
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList question

    Yes, that code is exactly what I want. I want an arraylist of arraylist. But, I don't know how to generate generic arraylist so that the names aren't predefined. The name of the arraylist is whatever the elements in the keys arraylist is, or in the code above, put those a,b,c,d,e, into the keys arraylist.
    1)
    ArrayList() Keys  = new ArrayList();
    Keys.add("A")
    Keys.add("B")
    Keys.add("C")
    2)
    for (int i =0  to i < Keys.size())
    Keys.get(i) = new ArrayList();
     
    //A would be its own ArrayList made from the Keys. so it's an Arraylist of Arraylist. 
    //I can't have any predefined list because the keys will be entered with random names. I just need to make a method that create ArrayList for every key elements. I don't know how to do that because I've tried to make a generic arraylist name but it doesn't work.

  7. #6
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: ArrayList question

    Well, I can't think of anyway of doing that with out making your own class that adds some addition info to your ArrayList.

    public class MyArray {
    String myName = "";
    ArrayList myList;
     
    	//constructor
    	public MyArray(String name){
    		myName = name;
    		//TODO, you will also want to initialize the list here by at least making a new ArrayList object.
    	}
    }

     
    // in your other main class.
    Arraylist keys = new ArrayList;
     
    //for (int i =0  to i < Keys.size())
    //Keys.get(i) = new MyArray();

    you will need to write new getter/setter methods to access your MyArray instance variables (myName, myList)
    EDIT: if you make them private, and you should.
    Last edited by JonLane; February 23rd, 2012 at 09:45 PM.

  8. The Following User Says Thank You to JonLane For This Useful Post:

    steel55677 (February 24th, 2012)

  9. #7
    Junior Member
    Join Date
    Jun 2011
    Posts
    29
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList question

    I'll go ahead and try that. I think I should be able to implement/extend ArrayList class. Thanks

  10. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: ArrayList question

    I'm making my own Hashmap but I don't know how to create individual ArrayList with varied names.
    Not sure why you wish to reinvent the wheel, but a HashMap is a LOT more complicated than creates Lists of Lists...it should utilize a hash function, buckets, and ways to expand the buckets - all together providing constant time look-up performance (as opposed to linear time look-up performance relying on looping over an ArrayList to find an element). My .02

  11. #9
    Junior Member
    Join Date
    Jun 2011
    Posts
    29
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList question

    Quote Originally Posted by copeg View Post
    Not sure why you wish to reinvent the wheel, but a HashMap is a LOT more complicated than creates Lists of Lists...it should utilize a hash function, buckets, and ways to expand the buckets - all together providing constant time look-up performance (as opposed to linear time look-up performance relying on looping over an ArrayList to find an element). My .02
    Because my teacher wants us to code our own code which means we can't use java's api other than arraylist. we had to make our own arraylist class from array. and i get that, i'm just making a simple hashmap to store keys and values and use %m to find the keys or horner's method. then use either quick sort or merge sort to find the keys. appreciate your .02, i wish we didn't have to reinent the wheels

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. Please help ArrayList Question
    By SandeeBee in forum Collections and Generics
    Replies: 14
    Last Post: November 15th, 2011, 12:01 AM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM