5

I have this:

private ArrayList<ArrayList<Object>> models;

and in the constructor I have:

models = new ArrayList<ArrayList<Object>>();

Later, I do things like:

models.add(new ArrayList<Object>());

as well as other operations.

I want to make the external ArrayList to something with a fixed size (Array, List) and I am really lost to how I am going to write the declaration, initialization, addition, etc. because of the nested objects. Can someone save me time by answering this for me?

7
  • 1
    Do you mean limit its size? Because if it's fixed-size, models.add would fail. Commented Jan 2, 2018 at 10:29
  • 1
    ArrayList is not fixed in size, arrays are. what do you mean? Commented Jan 2, 2018 at 10:30
  • Yeah something like: Array<ArrayList<Object>> models. I now the exact size of the outer "layer" Commented Jan 2, 2018 at 10:30
  • ArrayList<Object> [] array? Commented Jan 2, 2018 at 10:32
  • @JohnZobolas you can create a List meeting whatever requirements you need by extending AbstractList. Commented Jan 2, 2018 at 10:32

3 Answers 3

6

You can use Arrays.asList() to created fixed sized Lists.

Examples:

A List of size 3, initialized with null values:

List<ArrayList<Object>> models = Arrays.asList (null,null,null);

A List of size 3, initialized with non-null values:

List<ArrayList<Object>> models = Arrays.asList (new ArrayList<Object> (),new ArrayList<Object> (),new ArrayList<Object> ());

A List of size 10, initialized with null values:

List<ArrayList<Object>> models = Arrays.asList ((ArrayList<Object>[])new ArrayList[10]);

Note that add operation is not supported for fixed sized lists. You'll have to use models.set(index,new ArrayList<Object>()) instead.

EDIT:

Here's another way to initialize the List using Streams:

List<ArrayList<Object>> models = Arrays.asList (Stream.generate (ArrayList::new).limit (10).toArray (ArrayList[]::new));
Sign up to request clarification or add additional context in comments.

17 Comments

Once you get the list from list, you won't be able to modify it.
@ꜱᴜʀᴇꜱʜᴀᴛᴛᴀ You can replace the elements (the same way you can replace elements in arrays), you just can't add or remove elements.
you just can't add or remove elements. yeah. That is what I am trying to say :(
@ꜱᴜʀᴇꜱʜᴀᴛᴛᴀ which is exactly what the OP requested - I want to make the external ArrayList to something with a **fixed size**
@Zachary you can do something similar to what you suggested. I edited the answer.
|
2

A slightly more verbose - but generic-compatible way - of doing it is to extend AbstractList.

The methods you need to override are described in the Javadoc:

To implement an unmodifiable list, the programmer needs only to extend this class and provide implementations for the get(int) and size()methods.

To implement a modifiable list, the programmer must additionally override the set(int, E) method (which otherwise throws an UnsupportedOperationException).

So, implement these three methods, delegating to an ArrayList:

class FixedSizedList<T> extends AbstractList<T> {
  private final List<T> delegate;

  FixedSizedList(int size) {
    delegate = new ArrayList<>(Collections.nCopies(size, null));
  }

  public T get(int i) { return delegate.get(i); }
  public int size() { return delegate.size(); }
  public T set(int i, T e) { return delegate.set(i, e); }
}

Or, for that matter, just use an array:

class FixedSizedList<T> extends AbstractList<T> {
  private final Object[] array;

  FixedSizedList(int size) {
    array = new Object[size];
  }

  public T get(int i) { return (T) array[i]; }
  public int size() { return array.length; }
  public T set(int i, T e) {
    T old = (T) array[i];
    array[i] = e;
    return old;
  }
}

4 Comments

@Eugene no, it doesn't. That's the thing about extending AbstractList: it does the right thing for all the methods it doesn't ask you to override.
@Eugene when can you ever add elements by throwing an exception?
@Eugene it's a fixed-sized list: it never holds zero elements, unless you construct it to have zero elements. If you construct it with 10 elements, it stores 10 null elements initially.
oh my! I completly missed the Collections.nCopies(size, null) inside the constructor. :| will remove the comments
0

There isn't an Array class in java, but there is the plain old array declaration:

ArrayList<Object> [] array...

Problem is that you will not be able to instantiate it like this, since arrays can not be generic.

You are really looking for either Arrays.asList or much better ImmutableList of some kind.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.