Results 1 to 7 of 7
Thread: Declaring an ArrayList of Arrays
- 03-16-2010, 11:58 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 22
- Rep Power
- 0
[SOLVED] Declaring an ArrayList of Arrays
Hiya, I'm making a mock cache in Java for coursework. I have managed to write a direct-mapped cache using Arrays, but I want to use ArrayLists for a fully-associative cache. I am trying to declare an Arraylist of Arrays using two int variables to assign the intial length of the arraylist (numBlocks) and the length of the arrays within it (blockSize).
This is the statement:
However when I try to compile I get the error "']' expected". It must be something to do with passing in the blockSize variable because if I remove that I can compile. I'm using BlueJ if that matters, cheers.Java Code://removed
Last edited by mlad; 03-18-2010 at 02:26 PM.
- 03-17-2010, 12:25 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
you point out the error correctly. Here's what you need to do in order to initialize all arrays to the required size:
Java Code:public static ArrayList<int[]> cache = new ArrayList<int[]>(numBlocks); for (int i = 0; i < cache.size (); i ++) { cache.add (new int [blockSize]); }
- 03-17-2010, 01:18 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 22
- Rep Power
- 0
Thanks, this is helpful but I am now adding them as I need them instead (I am new-ish to Java so I am mostly using trial and error, and will probably try both ways in the end). Also, I have read that I can't store primitive types in ArrayLists, is this true? I am trying to create and ArrayList<int> and ArrayList<boolean> and they're giving me some hassle!
- 03-17-2010, 01:28 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
True. You need to use ArrayList<Integer> and ArrayList<Boolean>
Java Code:ArrayList<Integer> a = new ... int x = 48; a.add (new Integer (x)); // works a.add (x); // also works, although in the background it's really doing new Integer ()
-
Last edited by Fubarable; 03-17-2010 at 01:35 AM.
- 03-17-2010, 01:40 AM #6
Member
- Join Date
- Mar 2010
- Posts
- 22
- Rep Power
- 0
Ok I have changed that it is working now. Hopefully my last question, I am trying to assign the elements of the arrays (the ones in my ArrayList).
The above would be in a for loop, but doesn't work as it says array required, but int found.Java Code://removed
And populating a temporary array and assigning the ArrayList entry to it doesn't work as it is unexpected type. Could you tell me the correct syntax for the top piece please?Java Code://removed
Last edited by mlad; 03-18-2010 at 02:26 PM.
- 03-17-2010, 02:40 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
Accessing elements of an ArrayList which contains 2D arrays
By Willi in forum New To JavaReplies: 5Last Post: 01-18-2010, 07:00 AM -
Arraylist with arrays?
By Dieter in forum Advanced JavaReplies: 13Last Post: 09-19-2009, 11:10 PM -
Declaring a Queue
By rhm54 in forum New To JavaReplies: 1Last Post: 03-21-2008, 05:02 AM -
Declaring an ArrayList
By bugger in forum New To JavaReplies: 3Last Post: 01-31-2008, 07:36 PM -
Declaring Vector
By mew in forum New To JavaReplies: 1Last Post: 12-05-2007, 08:14 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks