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: ArrayList Help

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ArrayList Help

    I am sort of a hole on how to add miles and gas to the arraylist. Can i do something like this

    ArrayList<List> auto = new ArrayList<List>(6732, 83942, "Crown Victoria")

    is that a good start
    Below holds my code

     
     
    public class Garage {
     
    	private String model;
    	private int milesDriven;
    	private int GallonsGas;
     
    	public Garage(String ModelCar, int MilesDrove, int AmtGallonsGas)
    	{
    		model= ModelCar;
    		milesDriven= MilesDrove;
    		GallonsGas= AmtGallonsGas;
    	}
     
    	public String getModelCar()
    	{
    		String returnModelCar= model;
    		return returnModelCar;
    	}
     
    	public int getMilesDrove()
    	{
    		int returnMilesDrove= milesDriven;
    		return returnMilesDrove;
    	}
     
    	public int getAmtGallonsGas()
    	{
    		int returnAmtGallonsGas= GallonsGas;
    		return returnAmtGallonsGas;
    	}
     
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,370
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: ArrayList Help

    Study the ArrayList API. First note there is not a constructor that accepts the format you are trying to create. Seems to me you wish to create a new Garage object, then add that to the ArrayList (parametized with Garage).
    ArrayList<Garage> list = new ArrayList<Garage>();
    list.add(new Garage(...parameters to create a new garage go here...));

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ArrayList Help

    No, but you could create 3 parallel array lists that store them.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,370
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: ArrayList Help

    Quote Originally Posted by javapenguin View Post
    No, but you could create 3 parallel array lists that store them.
    Could be done this way as well, but java is object oriented and if those data pieces should be associated with one another, creating a single object to add to the list would be more appropriate.

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. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 03:08 PM
  3. ARRAYLIST
    By xs4rdx in forum Collections and Generics
    Replies: 4
    Last Post: March 25th, 2010, 11:36 AM
  4. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 03:08 AM
  5. [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, 02:11 PM