Results 1 to 12 of 12
  1. #1
    cpguy is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default ArrayList add method

    I have to create a class that stores ints, doubles, strings, and booleans in separate ArrayLists. The problem I'm having is that I'm supposed to write an overloaded add method for each type. This is what I have so far, but I have no idea what to do next. Thanks in advance!

    Java Code:
    import java.util.ArrayList;
    
    public class ValueStore
    {
       private ArrayList<Integer> intList = new ArrayList<Integer>();
       private ArrayList<Double> dblList = new ArrayList<Double>();
       private ArrayList<Boolean> boolList = new ArrayList<Boolean>();
       private ArrayList<String> stringList = new ArrayList<String>();
       
       public void add(int i)
       {  
       }
       
       public void add(double d)
       {
       }
       
       public void add(boolean b)
       {
       }
       
       public void add(String s)
       {
       }
       
    }

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: ArrayList add method

    Might want to visit ArrayList (Java Platform SE 6)

  3. #3
    cpguy is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: ArrayList add method

    Yes, I understand how the add method works, I just don't understand how to write my own.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: ArrayList add method

    What is add(int i), for instance, supposed to do? Whatever it is, you do that in the body of the method (presumably using the methods suggested by mine).

  5. #5
    cpguy is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: ArrayList add method

    Each add method is supposed to take in an int/double/boolean/String respectively, and add that to the end of the corresponding ArrayList. What I don't understand is how to write the code to add an int, for instance, to the end of the intList ArrayList.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: ArrayList add method

    What I don't understand is how to write the code to add an int, for instance, to the end of the intList ArrayList.
    The link you were given includes a method that does precisely that.

    Anything you can possibly ever do with intList you will do using one of the ArrayList's documented methods. You can write your own methods for your own class. But when it comes to using an instance of ArrayList<Integer> you use the methods it provides.

  7. #7
    cpguy is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: ArrayList add method

    This is the assignment: "Write the overloaded add methods (methods with the same name but different parameters), one that has an int parameter, one that has a double parameter, one that has a boolean parameter, and one that has a String parameter. All of the add methods should have a void return type. Each should add the value passed in to its associated generic ArrayList. Because of Java's automatic boxing and unboxing of primitive data types this should be relatively easy but remember that the primitive types can't actually be stored directly in the ArrayList and the the wrapper classes are involved even though the code does not use them explicitly (though it may - give it a try later). "

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default Re: ArrayList add method

    I'll do one method for you; you do the rest:

    Java Code:
       public void add(int i) { intList.add(i); }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    cpguy is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: ArrayList add method

    Well that was much easier than I thought it would be. My ValueStore file now looks like this. My problem now is that I need to create a Scanner in my main class. The Scanner scans in a user-input (can be int,double,bool,String) and adds it to the respective ArrayList in the other class. I'm having trouble passing the value to the other class.
    VALUE STORE:
    Java Code:
    import java.util.ArrayList;
    
    public class ValueStore {
    	private ArrayList<Integer> intList = new ArrayList<Integer>();
    	private ArrayList<Double> dblList = new ArrayList<Double>();
    	private ArrayList<Boolean> boolList = new ArrayList<Boolean>();
    	private ArrayList<String> stringList = new ArrayList<String>();
    
    	public void add(int i)
    	{
    	   intList.add(i);	
        }
    	   
    	public void add(double d)
    	{
    		dblList.add(d);
    	}
    	   
    	public void add(boolean b)
        {
    		boolList.add(b);
        }
    
    	public void add(String s)
        {
    		stringList.add(s);
        }
    
    	
    	
    }
    MAIN:
    Java Code:
    import java.util.Scanner;
    
    public class ValueStoreTest {
    	
    	public static void main(String[] args)
    	{
    		Scanner sc = new Scanner(System.in);
    		do {
    			System.out.println("Enter a value: ");
    			
    			if(sc.hasNextInt())
    			{
    				intList<>.add(sc.nextInt());
    			} else if (sc.hasNextDouble())
    			{
    				add(sc.nextDouble());
    			} else if (sc.hasNextBoolean())
    			{
    				add(sc.nextBoolean());
    			} else if (sc.hasNext())
    			{
    				if(!(sc.equals("quit)")))
    				{
    					add(sc.next());
    				}
    			}
    			
    		} while (!(sc.equals("quit")));
    		
    	}
    
    }

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default Re: ArrayList add method

    There are so many errors in there, I don't know where to start; here are some of the errors: that class doesn't have an intList, the other class has it as a member; b.t.w. what is "intList<>"? If the input String is "123.456" it starts with an int '123' but it also is a double '123.456', you have your tests in the wrong order. Where in that class is the reference to a ValueStore object that can do what you want?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    cpguy is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: ArrayList add method

    I've decided to break it down. Right now I'm just trying to focus on the add method for ints. Basically, what I want the program to do is prompt the user for input. As long as the input is not "quit", it keeps asking, and I want it to be adding these ints into my ArrayList IntList in the ValueStore class. I don't understand how to pass this int to the add method in the other class and to get it added to intList. THanks for your help!

    ValueStoreTest.java
    Java Code:
    import java.util.Scanner;
    import java.util.ArrayList;
    
    
    public class ValueStoreTest {
    	
    	public static void main(String[] args)
    	{
    		Scanner sc = new Scanner(System.in);
    		
    		System.out.println("Enter a value: ");
    		while (!(sc.hasNext("quit"))) 
    		{
    			System.out.println("Enter another value, or 'quit' to exit: ");
    			[WHAT DO I PUT HERE?].add(sc.nextInt());
    		} 
    	}
    
    }
    ValueStore.java
    Java Code:
    import java.util.ArrayList;
    
    public class ValueStore {
    	private ArrayList<Integer> intList = new ArrayList<Integer>();
    
    	public void add(int i)
    	{
    	   intList.add(i);	
            }
    }

  12. #12
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: ArrayList add method

    First, you have to declare and initialize ValueStore on ValueStoreTest.

Similar Threads

  1. ArrayList add method
    By GrahamButcher in forum New To Java
    Replies: 1
    Last Post: 11-01-2010, 11:51 AM
  2. add method from Arraylist - question
    By Adomini in forum New To Java
    Replies: 6
    Last Post: 10-21-2010, 07:08 PM
  3. need help with the remove method on arrayList
    By ShinTec in forum New To Java
    Replies: 5
    Last Post: 02-16-2010, 09:38 AM
  4. Calling a method when using an arraylist?
    By Jamison5213 in forum New To Java
    Replies: 10
    Last Post: 01-23-2010, 08:47 PM
  5. Replies: 3
    Last Post: 01-29-2009, 10:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •