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 4 of 4

Thread: Help making and iterating through ArrayList (Java Beginner)

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help making and iterating through ArrayList (Java Beginner)

    I am trying to make a list that contains four variable (integer, double, double, string). I need to create two methods. One method to add another entry to the list (but only if the integer variable is unique in the list), and another method that removes a specified entry (again, based on integer variable) from the list. I am having a lot of trouble setting up the ArrayList so that I can iterate through it in order to implement these two methods (I've tried a bunch of approaches, but nothing seems to be doing the trick). I appreciate any help with what I'm doing wrong!

    ArrayList collection=new ArrayList(); 	//initialization of trade collection
     
     
    	public void addTrade(int identifier, double amount, double rate, String currency){
    		//addTrade: inserts a single FXTrade instance into the collection
    		for (int i = 0; i < collection.size(); i++){
    			//ensure the instance is unique before adding to the list
     
    			Object currElement = collection.get(i);
    			if (currElement == collection(identifier, amount, rate, currency)){  //don't add the new trade instance if it's already in the collection
    				continue;
    			}else {
    				collection.add(identifier, amount, rate, currency);
    			}		
    		}
    	}
     
     
    	public void removeTrade(int identifier){
    		//removeTrade: removes a single FXTrade instance from the collection
     
    		//search collection for requested trade; remove the requested trade (if it's in the collection)
    		for (int i = 0; i < collection.size(); i++){
    			if (collection.get(i)==identifier){
    				collection.remove(i);
    				break;  //once the trade is removed, stop iteration (uniqueness was checked in addTrade method)
    			}
    		}
    	}


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help making and iterating through ArrayList (Java Beginner)

    You need to create an Object that encapsulates those variables. It doesn't make sense to add all of them to the List at once- you need to put them into an Object, then add that Object to the List.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help making and iterating through ArrayList (Java Beginner)

    Could you give me an example of how to do that? I am trying to input and store those four pieces of information, and am stuck on how to go about doing it.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help making and iterating through ArrayList (Java Beginner)

    Recommended reading: Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] Iterating through an ArrayList
    By Actinistia in forum Collections and Generics
    Replies: 2
    Last Post: October 31st, 2011, 02:19 PM
  2. Iterating a String
    By av8 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: July 7th, 2011, 12:37 PM
  3. While loop not iterating correctly
    By Rhyssa6 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 18th, 2011, 09:13 PM
  4. [HELP] Iterating through a jagged edged array
    By AlexBeer in forum Collections and Generics
    Replies: 2
    Last Post: April 12th, 2010, 06:33 AM
  5. ArrayList and making a Menu
    By Silver in forum Collections and Generics
    Replies: 2
    Last Post: January 28th, 2010, 03:29 AM

Tags for this Thread