Results 1 to 8 of 8
  1. #1
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default ArrayList Help!?

    Ok, well, first off, let me tell you what im trying to do, i have a program where i add numbers to an arraylist, then, i organize the arraylist so, i have for example, 10 elements in the array list:

    arraylist list1;

    Before: [1, 12, 21, 31, 32, 13, 1, 32, 31, 32, 31, 2]

    After: [1, 1, 2, 12, 13, 21, 31, 31, 31, 32, 32, 32]

    I can do that part simplle, then i devide the array list into 3, so in this case i get

    m1 = 0-3
    m2 = 4-7
    m3 = 8-11

    now, here is where im problem lies, i want to take all the elements 0, 1, 2, 3 of list1, then average all of them. I tried the list1.get() meathod, but i cant return the elements in the array list as int's. in this example, the average would be 7.00 (i would be using doubles) but i cannot get values of the arraylist. please help!

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    List1.get() returns an object. so, int x = list.get(2); i want it to give me, what ever value is stored in element 2, for example the value 10, so i would want x = 10, but instead i get it saying that it doesn't work.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Are you allowed to use generic lists such as ArrayList<Integer>? If so, if you use this, then get will return an Integer (or you can change this to Double if desired). If you are not allowed to use this, then you will have to cast the object returned to Integer (or Double if that's what the list is holding) and call the getValue method on the casted object.

    I strongly recommend that you use generic if possible.

  5. #5
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Ok, few questions with that? "ArrayList<double> list1 = new ArrayList<double>();" doesnt work, haha so im missing something. but as far as integer goes, can you change integer? Im not sure how to call a generic list.

  6. #6
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Oh wow haha syntax error, spelling of double was wrong, i needed Double no double. haha I will look into the ways you told me just so i know them for future referance.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    Where do the JTables and JButtons and KeyListeners come in? This is the Swing forum ...

    kind regards,

    Jos

  8. #8
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    I tried the list1.get() meathod, but i cant return the elements in the array list as int's.
    Did you try something like:
    Object obj = list1.get(ix);
    System.out.println("===> type = " + obj.getClass().getSimpleName().toString());
    if (obj isinstanceof Integer){
    Int i = (Integer) obj;
    System.out.println("===> i = " + i);
    }

Similar Threads

  1. Creating an ArrayList from an ArrayList
    By Klahking in forum New To Java
    Replies: 17
    Last Post: 09-09-2010, 03:34 PM
  2. Arraylist
    By gnarly hogie in forum New To Java
    Replies: 2
    Last Post: 12-11-2008, 01:59 AM
  3. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  4. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 08:48 PM
  5. New to arraylist
    By kleave in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 06:45 PM

Posting Permissions

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