Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: Arraylist

  1. #1
    chdn20 is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default Arraylist

    Hello all Iím trying to write a program where I increase only one field in an arraylist. My arraylist looks something like:
    cars.add(new Cars("Honda",20000, 13400.25,));
    cars.add(new Cars ("Nissan",10000,18000.45,));
    cars.add(new Cars ("BMZ", 25000, 6100.00));
    cars.add(new Cars ("Ford", 18000, 28000.28,));
    cars.add(new Cars ("GM",1700,10000.25,));

    So Iím using a find method so the user can search by type(IE Honda) then java asks to add mileage. I know how to add a new car but I donít know how to increase a field in the arraylist. Any help would be great.

    Thanks

    public void addCurrent(ArrayList<Cars> st)
    {

    Scanner scan = new Scanner(System.in);
    String location = findItem(st);

    System.out.println("How many miles would you like to add?");
    int more = scan.nextInt();

    ???
    System.out.println ("added");
    }

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    ArrayList (Java Platform SE 6) - Array List API
    ArrayList (Java Platform SE 6) -Arraylist Contains

    And for editing the arraylist item, just myArrayList[myArrayListSpot].addMileage(milesToAdd); should suffice.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Java Code:
     myArrayList[myArrayListSpot].addMileage(milesToAdd);
    This looks like an array reference: [] vs using the method get() for an ArrayList

    how to increase a field in the arraylist
    First you'd need to find the index of the object you want to update. Then using that index you'd use get() to get a reference to the object so you could call that object's method to update its data.

  4. #4
    chdn20 is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default

    Thanks. So what I was trying to do was use the get() with location but location is a string input so I was getting an error. Should I take location and get the reference location then use that?

    st.get(location).setMileage(st.get(location).getMi leage() +more);

    Thanks

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    What is that statement supposed to do? Can you break it down to a bunch of single steps?
    Is this what you are trying to do?
    obj1 = st.get(location); // get the object at location.
    obj2 = st.get(location); // get another reference to the same object????
    int mileage = obj2.getMileage(); // get the mileage for obj2
    mileage = mileage + more;
    obj1.setMileage(mileage);

  6. #6
    chdn20 is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default

    Its supposed to ask for the type (IE honda, GM) then as ask to add mileage to the total mileage then update.

    I can send or post the .jar file if needed.

    Thanks

  7. #7
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Your method to add miles to the car should be simpler then that, instead of trying to set the mileage to a certain mileage each time just make an addMileage method. It should just be something like:
    Java Code:
    public void addMileage(int miles)
    {
         mileage += miles;
    }
    At least that what I think you're trying to do.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  8. #8
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Honestly I'm not really sure if this is the best way to do what you're trying to accomplish, but I tried something like this:

    Java Code:
    for (int i=0; i<myArray.size();i++)
    {
         if((myArray.get(i).getName()).equals(location))
              myArray.get(i).addMileage(2000);
    }
    I'm thinking there might be a more efficient way to do it with .contains(), but I can't really figure out how right now. In your program this should work just fine, but in a larger program with say like 3000-4000 cars on the lot you might see some performance issues.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    The contains() method uses the equals() method which can look for the same object vs an object with the same contents. With a large number of items, a Map might be a better container for quick look ups.
    Or you could override the class's equals() method. That can be tricky if the object is used in some containers that use hashcode.

    For student exercises, none of this matters.

  10. #10
    chdn20 is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default

    Thanks. I got my method to update the mileage know I'm just trying to find a better way to find the index of the value. I keep getting an outofbounds error because its retuning an index of -1 by default. Does this look rights??

    public int find(ArrayList<Cars> st) // menu item #6
    {
    Scanner scan = new Scanner(System.in);
    System.out.println(" What is the type ");
    String num = scan.nextLine();// find a stock by id code
    {
    boolean foundIt = false;

    for(int i = 0; i < st.size(); i++){
    Cars s = st.get(i);
    String n = s.getId();

    if (n.equalsIgnoreCase(num)){
    System.out.println(s.toString());
    foundIt = true;
    int idx = st.indexOf(num);
    return idx;
    }

    }
    if (!foundIt)
    System.out.println("The car is not found.");
    int idx = st.indexOf(num);
    return idx;


    } // write the code to do the item depending on their choice
    public void addCurrent(ArrayList<Cars> st)
    {

    Scanner scan = new Scanner(System.in);
    int location = find(st);
    System.out.println("How many would you like to add?");
    int more = scan.nextInt();
    int idx = st.indexOf(location);
    st.get(idx).setNumShares(st.get(idx).getNumShares( ) +more);

    System.out.println ("added");

  11. #11
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    How would I use .contains instead of the way I did it? Just because I'm curious and I think the OP would benefit from the explanation too.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Does this look rights??
    First you should let the compiler check it.
    And then let the java program execute it.
    Look at the output and if its what you want, that's it.
    If it is not what you want and you can't see why,
    copy and paste here the output from the program along with an explanation of what is wrong with the output and with an example of what the output should be.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Java Code:
    if (n.equalsIgnoreCase(num)){  // test if n equals num  ??? what is n, what is num
      System.out.println("s=" + s.toString());  //  Show current object
      foundIt = true;     // remember that we have found it
      int idx = st.indexOf(num);  // ??? WHAT DOES THIS DO???
      return idx;
    }
    Please fix the comments explaining what it is supposed to do.
    Last edited by Norm; 07-08-2011 at 04:51 PM.

  14. #14
    chdn20 is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default

    The output looks like:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.ArrayList.get(ArrayList.java:324)
    at new.MenuInfo.addCurrent(MenuInfo.java:404)
    at new.MenuInfo.addStock(MenuInfo.java:389)
    at new.StockDriver.main(StockDriver.java:63)

    The problem is in the st.indexOf section. I’m trying to take in a String variable (num) which would be the type of car (GM Ford).
    System.out.println(" What is the type ");
    String num = scan.nextLine();// find a stock by id code

    Then find the index value so I can update the mileage.
    int idx = st.indexOf(num);
    return idx:
    So idx should be the location of the type of car
    I then put use idx to update the mileage.
    int idx = st.indexOf(location);
    st.get(idx).setNumShares(st.get(idx).getNumShares( ) +more);
    However, int idx is set to -1 which causes my error.

    Thanks

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Have you read the API doc for the indexOf() method?
    When does it return a -1?
    Why do you think the object: num is in the ArrayList? That is what indexOf(num) looks for. The element that is the same as num.
    num is a String. One or more of the objects in the ArrayList could contain a reference to a String with the same contents as num.

    What do you want the find() method to return?
    What is the value of i when the desired object is found?

  16. #16
    chdn20 is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default

    Yea, I looked at the API and it returns -1 when there is no such index. Which leads me to think I messed up the indexOf(). If I just put a number such a 1 in the indexof() , then the problem runs and updates the mileage that corresponds to 1 so at least I know that works.

    I want find return the index location so I can use my addCurrent() to update the mileage. i should correspond to what ever equals the vehicle type in the array list.
    IE.
    If Honda is searched for, it should return index 0. If Nissan then 1.

    cars.add(new Cars("Honda",20000, 13400.25,));
    cars.add(new Cars ("Nissan",10000,18000.45,));
    cars.add(new Cars ("BMZ", 25000, 6100.00));
    cars.add(new Cars ("Ford", 18000, 28000.28,));
    cars.add(new Cars ("GM",1700,10000.25,));

    Thanks so much.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    I can't tell from what you said.
    Does your find() method work now?

    did you see the end of post#15

  18. #18
    chdn20 is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default

    No the Find() doesn’t work. So far my find() looks like this:
    public int find(ArrayList<Cars> st) // menu item #6
    {
    Scanner scan = new Scanner(System.in);
    System.out.println(" What is the id code ");
    String num = scan.nextLine();// find a stock by id code
    {
    boolean foundIt = false;

    for(int i = 0; i < st.size(); i++){
    Stock s = st.get(i);
    String n = s.getId();

    if (n.equalsIgnoreCase(num)){
    System.out.println(s.toString());
    foundIt = true;
    int idx = st.indexOf(num);
    System.out.println(" location = " + st.indexOf(num));
    System.out.print(num);
    return idx;


    I put in the two System.out.println’s to help troubleshoot. I’m taking in the num value (which is a String ) in order to use it to find the index location so I can update it with the mileage.
    The output looks like
    -1
    HONDA

    So it has the HONDA but the indexOf isn’t finding the index value.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Did you see my comments at the end of post#15?

  20. #20
    chdn20 is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default

    Just got them:

    What do you want the find() method to return?
    I want it to return the index location of the searched for car.
    What is the value of i when the desired object is found?


    OHH.. I think i just figured it out.
    I can use i to find my index value and cut out all this non-sense!!

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  2. copying contents of an ArrayList to another ArrayList
    By ankit1801 in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 06:07 AM
  3. sorting arraylist based on another arraylist
    By busdude in forum New To Java
    Replies: 4
    Last Post: 02-07-2011, 11:48 AM
  4. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •