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

Thread: Edit ArrayList Object

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Edit ArrayList Object

    Deleted since my college will scan using turnitin.com
    Last edited by frankycool; November 16th, 2009 at 11:57 AM. Reason: Plagarism


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Edit ArrayList Object

    I suspect it may be because you have the variable that holds the inventory declared static inside of CarsSubProduct. Change it to be non-static, and then you can get the object reference using the ArrayList.get() method:

    CarsSubProduct car = products.get(0); // get the first car in the list
    car.soldOne(); // decrement the inventory of car by one

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Edit ArrayList Object

    Thanks,i tried your method and now i can specify which object i want to get in the array like


    int price=Cars.get(0).getprice();

    But now i want to search all the indexes and get the index which contains for example Ford_Mustang then i use another syntax as such above to get the price

    Cars.add( new CarMethods( "Ford_Mustang",1,1000,10000 ) );

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Edit ArrayList Object

    Override the equals method of your CarsSubProduct class (or whatever you are putting into the ArrayList) so that it will test equality by the name:

    public boolean equals(Object o)
    {
         if (o instanceof String)
         {
              return (this.name.equals(o.toString());
         }
         else
         {
              return false;
         }
    }

    Then assuming each ArrayList you have will only have 1 Ford_Mustang, you can use the indexOf method to find it:

    int index = car.indexOf("Ford_Mustang");

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    frankycool (November 15th, 2009)

  6. #5
    Junior Member
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Edit ArrayList Object

    Thanks will do as explained

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Edit ArrayList Object

    indexOf returns -1 if it doesn't find the object you want.

  8. #7
    Junior Member
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Edit ArrayList Object

    Even when i initialize Carname and run on netbeans debugger,Carname still appears as null

  9. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Edit ArrayList Object

    Post your CarName class... I have a feeling the error is somewhere in there.

  10. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Edit ArrayList Object

    I need the CarMethods class. Could you post that as well?

  11. #10
    Junior Member
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Edit ArrayList Object

    Deleted since my college will scan using turnitin.com
    Last edited by frankycool; November 16th, 2009 at 11:45 AM. Reason: Plagarism

  12. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Edit ArrayList Object

    You forgot to over-write the the equals method in the CarMethods class

    public boolean equals(Object o)
    {
         if (o instanceof String)
         {
              return (this.name.equals(o.toString());
         }
         else
         {
              return false;
         }
    }

  13. #12
    Junior Member
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Edit ArrayList Object

    Deleted since my college will scan using turnitin.com
    Last edited by frankycool; November 16th, 2009 at 11:46 AM. Reason: Plagarism

  14. #13
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Edit ArrayList Object

    hmm.. it seems that the indexOf method compares the types first (kind of annoying )

    An easy way around this is to create a "dummy" CarMethods object that has the name, but nothing else.

    So:

    Cars.indexOf(new CarMethods("Ford_Mustang",0,0,0));

    You'll also need to change the .equals() method to this:

    	public boolean equals(Object o)
    	{
    		if (o instanceof String)
    		{
    			return (this.carsnames.equals(o));
    		}
     
    		else if (o instanceof CarMethods)
    		{
    			return (this.carsnames.equals(((CarMethods) o).carsnames));
    		}
    		else
    		{
    			return false;
    		}
    	}
    will give you the indexOf the first Ford_Mustang in the ArrayList. The alternative is to iterate through the entire ArrayList of Cars, which isn't too hard.

    public int customIndexOf(ArrayList<CarMethods> cars, String toFind)
    {
         for (int i = 0; i < cars.size(); i++)
         {
              if (cars.get(i).equals(toFind))
               {
                   return i;
              }
         }
         return -1;
    }

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. Struts combobox edit
    By kalees in forum Web Frameworks
    Replies: 2
    Last Post: November 1st, 2009, 12:27 AM
  3. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 02:08 AM
  4. [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, 01:11 PM
  5. How to edit eclipse project builds
    By akhilachuthan@yahoo.co.in in forum Java IDEs
    Replies: 1
    Last Post: June 25th, 2009, 09:36 AM

Tags for this Thread