Results 1 to 12 of 12
Thread: ArrayList add method
- 10-06-2011, 03:55 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
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) { } }
- 10-06-2011, 04:03 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Re: ArrayList add method
Might want to visit ArrayList (Java Platform SE 6)
- 10-06-2011, 04:34 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
Re: ArrayList add method
Yes, I understand how the add method works, I just don't understand how to write my own.
- 10-06-2011, 05:16 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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).
- 10-06-2011, 05:20 AM #5
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
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.
- 10-06-2011, 05:31 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: ArrayList add method
The link you were given includes a method that does precisely that.What I don't understand is how to write the code to add an int, for instance, to the end of the intList ArrayList.
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.
- 10-06-2011, 05:34 AM #7
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
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). "
- 10-06-2011, 08:49 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Re: ArrayList add method
I'll do one method for you; you do the rest:
kind regards,Java Code:public void add(int i) { intList.add(i); }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-06-2011, 11:56 AM #9
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
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:
MAIN: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); } }
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-06-2011, 01:00 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-07-2011, 02:05 AM #11
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
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
ValueStore.javaJava 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()); } } }
Java Code:import java.util.ArrayList; public class ValueStore { private ArrayList<Integer> intList = new ArrayList<Integer>(); public void add(int i) { intList.add(i); } }
- 10-07-2011, 02:39 AM #12
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Similar Threads
-
ArrayList add method
By GrahamButcher in forum New To JavaReplies: 1Last Post: 11-01-2010, 11:51 AM -
add method from Arraylist - question
By Adomini in forum New To JavaReplies: 6Last Post: 10-21-2010, 07:08 PM -
need help with the remove method on arrayList
By ShinTec in forum New To JavaReplies: 5Last Post: 02-16-2010, 09:38 AM -
Calling a method when using an arraylist?
By Jamison5213 in forum New To JavaReplies: 10Last Post: 01-23-2010, 08:47 PM -
How can I call my database read method to display its ArrayList?
By matpj in forum New To JavaReplies: 3Last Post: 01-29-2009, 10:20 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks