4

When you create an arraylist of type Integer in Java what are the default values? I need to check if an arraylist is full and I was going to get the size of the array then get the value at the last index and check if it was the default value.

Is there a better way? What would be the default value?

Hope that makes sense. Cheers

int size = a.size();
int last = a.get(size);
if( last == null )
{
    return true;
}else{
    return false;
}

Edit;

Is it possible to create an ArrayList with a max size that you can not go over to stop it dynamically expanding? When you create an ArrayList and you use size() would that return the actual size or the amount of elements in the arraylist? When doing this to create a max size would the default values be null?

public boolean isFull()
{
    int size = a.size();
    int last = 0;
    try{
        last = a.get(size-1);
    }catch (Exception e){
    }
    if( last == null )
    {
        return true;
    }else{
        return false;
    }
}

I currently have this, how does it look? Does this make sense now?

7
  • 1
    Are you working with an array, a java.util.ArrayList, or something else? Commented Mar 1, 2013 at 16:47
  • 2
    An ArrayList is never "full". And there is no default value either, the default is the list is "empty". Your question makes no sense. Commented Mar 1, 2013 at 16:47
  • AaronKurtzhals Yes util arraylists. @Durandal, Ah right. I'd forgotten about that. Would Array be the correct implementation for this? Commented Mar 1, 2013 at 16:49
  • @Kyle93 Reformulate your question to make clear what problem you want to solve. Your sample code piece makes no sense either, it will inevitably throw an IndexOutOfBoundsException at the 2nd line. Commented Mar 1, 2013 at 16:55
  • @Durandal I have done so, I hope that makes sense. Commented Mar 1, 2013 at 17:07

8 Answers 8

4

When you declare an ArrayList it is empty. It is also a dynamic container meaning it will grow so for you to ask if it is "full" is more of a constraint you'd need to add to your code.

So, if you want to achieve a goal like you describe.

List<Integer> list = new ArrayList<Integer>();
int MAX_ELEMENTS = 5; //Set this to however you want to constrain the datatype

public boolean addElement(Integer value) {
   if (list.size() < MAX_ELEMENTS) {
     list.add(value);
     return true;
   } else {
     return false;
   }
}

public boolean isFull() {
  return (list.size() == MAX_ELEMENTS);
}

public Integer getLast() {
  if (!list.isEmpty())
    return list.get(list.size()-1);
  else
    return null;
}

As others have stated though, if you generate a list with a preset size as such:

List<Integer> list = new ArrayList<Integer>(10);

You'd have a list of 10 elements large all being null in value. Should you add additional elements the list will still grow larger than 10 elements unless you constrain it like I did above.

Sign up to request clarification or add additional context in comments.

1 Comment

It's wrong and misleading to say the list initialised with capacity has 10 elements that are null in value. The array that backs the ArrayList does indeed have null values there, but the logical list does not yet have any elements. A null element is very different from a nonexisting element. Don't mix up capacity and size.
3

If you haven't actually added Integers to the ArrayList, then any get() on the list will return an IndexOutOfBoundsException.

The size() method returns the number of elements in the list (i.e. how many you have added to it), not the current capacity.

Comments

3

By default ArrayList capacity is 10. All of them are null by default until you add your elements into it. But calling size() will give you number of elements that you have added. It wont give 10 as result(default null values will not be considered). Twist here is if you add null values to the list then they are included while calculating size(). Example if you add 3 valid Integers and 2 null values into the list then size() will return 5. Eclipse debugging will help you in finding this dynamic increasing of its capacity.

Comments

2

When you create an ArrayList, inside the ArrayList class, there is an array of elements. Those elements are set to null because they do not refer to any instance of an Integer object. Bare in mind, that isn't the same as an int.

Moreover, an ArrayList doesn't get full. It is dynamic, and will increase in size when it needs to.

Edit: in response to your edit about setting a maximum size, if you want a maximum size then I'm not sure why you'd want an arraylist. But if you want to stick with an ArrayList class, I would create my own class that is a subclass of arraylist, and override the add method with a check to ensure the value of size() isn't over a fixed amount.

2 Comments

size() is the logical indicator of the content, whatever the implementation. if it is 0, there is "nothing". Those elements you are talking about are not accessible so don't exist.
Yes. I didn't say the value of size() will increase. I said the array itself will increase in size, which it will.
1

There is not such thing like "full" ArrayList. The size() method will return the number of elements it currently holds.

4 Comments

Do we really consider it as null? I would say there is no default since there is not object.
If I was to create an ArrayList with an initial size of 10, then added 3 values to it. The size would still be 10 not 3 correct?
No, the size will be 3. When you do new ArrayList<Integer>(10); 10 is the initial capacity, not the initial size.
@Dan Ah thanks! My fault! I'll take another look over ArrayLists and there applicable methods and how they work
1

Do you want to simply constraint the list to a given size, or do you want to check if it is larger than a given size?

Check if list is larger than:

if (list.size() > limit)
    System.out.println("List too large");

Its not possible to constraint the size of an ArrayList - you can however create your own subclass of ArrayList that does just that:

public class LimitedList<E> extends ArrayList<E> {

     private int limit;

     public LimitedList(int limit) {
         this.limit = limit;
     }

     public boolean add(E e) {
         // only add if the limit is not exceeded
         if (size() < limit)
             super.add(e);
     }

     // overwriting the addAll()-methods is left as an excercise to the reader
}

You only need to decide what the list should DO when one attempts to add more elements than the limit allows. Either just ignore the elements or throw an Exception.

Comments

0

ArrayLists have no default values. If you give the ArrayList a initialCapacity at initialization, you're just giving a hint for the size of the underlying array—but you can't actually access the values in the ArrayList until you add the items yourself.

Comments

0

Any List implementation has a isEmpty() method you can use.

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.