Results 1 to 9 of 9
Thread: Help with ArrayList
- 01-15-2013, 08:09 PM #1
Member
- Join Date
- Apr 2012
- Location
- St. Louis, MO
- Posts
- 32
- Rep Power
- 0
Help with ArrayList
I've been assigned a program in which I make a Spy Class, each of which has certain perameters, including an array list of aliases. Furthermore, there are SpyRings, which contain spies. A final driver class prints out all elements of a spyring. When I attempt to print the SpyRing, it compiles w/ no errors, but prints nothing. Any help?
Here's the Spy Class:
Here's the SpyRing class:Java Code:import java.util.*; public class Spy { private String inname; private String inspyNum; private static ArrayList<String> aliases; public Spy(String spyNum, String name) { inname=name; inspyNum=spyNum; aliases = new ArrayList<String>(); } public String toString() { return "Name: "+inname +" Number: "+inspyNum+" Aliases: "+aliases; } public static void addAlias(String newAlias) { aliases.add(newAlias); } public String getName() { return inname; } public String getSpyNum() { return inspyNum; } public ArrayList<String> getAliases() { return aliases; } }
Here's the driver class:Java Code:import java.util.*; public class SpyRing { private static ArrayList<Spy> Spies; private String incountry; public SpyRing(String country) { Spies = new ArrayList<Spy>(); incountry=country; } public void addSpy(String spynum, String spyname) { Spy x=new Spy(spynum, spyname); Spies.add(x); } public static ArrayList<Spy> getSpyList() { return Spies; } }
Java Code:import java.util.*; import cs1.Keyboard; public class SpyRingMaker { public static void main (String[]args) { boolean contin=true; boolean continuity=true; int count=0; while(contin) { System.out.println("1. Enter a spy for your SpyRing"); System.out.println("2. Print SpyRing"); System.out.println("3. Quit"); int choice=Keyboard.readInt(); String sNum, sName; SpyRing Goldfinger= new SpyRing("Australia"); switch(choice)xz { case 1: System.out.println("Enter the name of the Spy"); sName=Keyboard.readString(); System.out.println("Enter the Number of the Spy"); sNum=Keyboard.readString(); Goldfinger.addSpy(sNum, sName); Goldfinger.getSpyList().get(count); while(continuity) { System.out.println("1. Enter an alias for this spy"); System.out.println("2. Done with this Spy"); int choices=Keyboard.readInt(); switch(choices) { case 1: System.out.println("Enter an alias"); String sAlias=Keyboard.readString(); Goldfinger.getSpyList().get(count).addAlias(sAlias); break; case 2: continuity=false; break; } count++; } break; case 2: for (Spy each: Goldfinger.getSpyList()) { System.out.println(each); } break; case 3: System.out.println("Goodbye"); contin=false; break; default: break; } } } }
- 01-15-2013, 09:22 PM #2
Re: Help with ArrayList
Where is the code that is trying to print the SpyRing? Are there any elements in the ArrayList to be printed? If not where does the code add any elements to the arraylist? If the code adds some elements to an ArrayList, is it the same ArrayList as the one being used for printing?When I attempt to print the SpyRing, it compiles w/ no errors, but prints nothing
Try debugging the code by adding some println statements to show what is added, how many elements are in the arraylist after each add.If you don't understand my response, don't ignore it, ask a question.
- 01-17-2013, 05:42 AM #3
Member
- Join Date
- Apr 2012
- Location
- St. Louis, MO
- Posts
- 32
- Rep Power
- 0
Re: Help with ArrayList
Norm, I try to print the SpyRing in lines 54-61 of the Driver.
In answer to your second question, the SpyRing is an ArrayList of Spies, which contains an ArrayList of aliases. So yes, there are elements to be printed.
As for the last question, yes--I believe that the program is printing the ArrayList that I'm putting elements into
- 01-17-2013, 12:49 PM #4
Re: Help with ArrayList
Did you try debugging the code by adding calls to the println method as I suggested?
Add a println in the constructors so you can see when they are called.
How many items are in the array list when you print it at different places in the code?Last edited by Norm; 01-17-2013 at 01:47 PM.
If you don't understand my response, don't ignore it, ask a question.
- 01-17-2013, 01:38 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
Re: Help with ArrayList
When I compile, I get some warnings. I suggest that you compile with warnings turned on and try to understand the warnings. Fixing your code to make all warnings go away often fixes bugs that make it past the compiler.When I attempt to print the SpyRing, it compiles w/ no errors, but prints nothing.
- 01-17-2013, 08:12 PM #6
Member
- Join Date
- Apr 2012
- Location
- St. Louis, MO
- Posts
- 32
- Rep Power
- 0
- 01-17-2013, 08:24 PM #7
Re: Help with ArrayList
Where does it happen, what is the value of the index and how long is the item that is being indexed?I get an indexOutOfBoundsException
The cause is the index's value is too big for the item being indexed.If you don't understand my response, don't ignore it, ask a question.
- 01-22-2013, 07:46 PM #8
Member
- Join Date
- Apr 2012
- Location
- St. Louis, MO
- Posts
- 32
- Rep Power
- 0
Re: Help with ArrayList
It happens when adding a second element to the Aliases ArrayList.
- 01-22-2013, 08:00 PM #9
Similar Threads
-
ArrayList copy some of the element from one arraylist tnto another arraylist
By ralf in forum New To JavaReplies: 12Last Post: 07-07-2011, 08:49 PM -
Let ArrayList point to another arraylist but with a filter.
By gonzalioz in forum Advanced JavaReplies: 1Last Post: 05-15-2011, 06:07 PM -
copying contents of an ArrayList to another ArrayList
By ankit1801 in forum New To JavaReplies: 8Last Post: 03-27-2011, 06:07 AM -
how to add Arraylist filter for a jsp page showing results from a servlet-Arraylist
By alok_sharma in forum Java ServletReplies: 7Last Post: 11-22-2010, 01:26 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks