Results 1 to 7 of 7
  1. #1
    mlad is offline Member
    Join Date
    Mar 2010
    Posts
    22
    Rep Power
    0

    Post [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:

    Java Code:
    //removed
    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.
    Last edited by mlad; 03-18-2010 at 03:26 PM.

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    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]);
    }

  3. #3
    mlad is offline Member
    Join Date
    Mar 2010
    Posts
    22
    Rep Power
    0

    Default

    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!

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    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 ()

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

    Default

    Quote Originally Posted by mlad View Post
    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!
    Yep, what you heard is correct, since ArrayLists (all collections) hold objects only. If you need a collection of ints, you'll have to use the Integer wrapper class instead, same for Boolean/boolean.

    Edit: I'm sloooow tonight! It must be my age...
    Last edited by Fubarable; 03-17-2010 at 02:35 AM.

  6. #6
    mlad is offline Member
    Join Date
    Mar 2010
    Posts
    22
    Rep Power
    0

    Default

    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).

    Java Code:
    //removed
    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?
    Last edited by mlad; 03-18-2010 at 03:26 PM.

  7. #7
    mlad is offline Member
    Join Date
    Mar 2010
    Posts
    22
    Rep Power
    0

    Default

    I have figured it out now, its cache.get(g)[i] for future reference.

    All problems solved, thanks everyone!!

Similar Threads

  1. Replies: 5
    Last Post: 01-18-2010, 08:00 AM
  2. Arraylist with arrays?
    By Dieter in forum Advanced Java
    Replies: 13
    Last Post: 09-20-2009, 12:10 AM
  3. Declaring a Queue
    By rhm54 in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 06:02 AM
  4. Declaring an ArrayList
    By bugger in forum New To Java
    Replies: 3
    Last Post: 01-31-2008, 08:36 PM
  5. Declaring Vector
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-05-2007, 09:14 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
  •