Results 1 to 8 of 8
Thread: ArrayList Help!?
- 10-26-2010, 01:35 AM #1
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
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!
- 10-26-2010, 04:58 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
get() with the correct index return the value of the list. Where you stuck with?
- 10-26-2010, 04:55 PM #3
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
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.
-
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.
- 10-26-2010, 05:02 PM #5
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
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.
- 10-26-2010, 05:08 PM #6
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
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.
- 10-26-2010, 06:00 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Where do the JTables and JButtons and KeyListeners come in? This is the Swing forum ...
kind regards,
Jos
- 10-27-2010, 06:05 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 63
- Rep Power
- 0
Did you try something like:I tried the list1.get() meathod, but i cant return the elements in the array list as int's.
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
-
Creating an ArrayList from an ArrayList
By Klahking in forum New To JavaReplies: 17Last Post: 09-09-2010, 03:34 PM -
Arraylist
By gnarly hogie in forum New To JavaReplies: 2Last Post: 12-11-2008, 01:59 AM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
ArrayList
By kizilbas1 in forum New To JavaReplies: 1Last Post: 01-12-2008, 08:48 PM -
New to arraylist
By kleave in forum New To JavaReplies: 2Last Post: 11-19-2007, 06:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks