简体   繁体   中英

Why my following program does not print any output ( ArrayList )?

import java.util.ArrayList;
public class test 
{
        public static void main(String[] args)
        {
            ArrayList<Integer> numo = new ArrayList<Integer>();

            for(int i=0;i<numo.size() ;i++)
            {
            
                numo.add(i);
            }

            for(int i=0;i<numo.size() ;i++)
            {
            
            System.out.println(nuenter code heremo.get(i));
            }
    
        }       
}

The code is working, but it does not print anything when I run it. I want to print all the integers in the arraylist. I'm new to Java.

You may tryto add some values to your numo list and then loop over to print those values

import java.util.ArrayList;
public class test 
{
        public static void main(String[] args)
        {
            ArrayList<Integer> numo = new ArrayList<Integer>();

            for(int i=0;i<10 ;i++)
            {
                // add some values to numo array list
                numo.add(i);
            }

            for(int i=0;i<numo.size() ;i++)
            {
            //print those values
            System.out.println(nuenter code heremo.get(i));
            }

        }       
}

Your list size is zero initially, so you are not able to iterate the list and print anything.

Also don't try to iterate and add items to the same list (without proper condition to terminate the loop) , which will generate infinite loop ,

So add elements to the list first and then iterate it as shown below:

ArrayList<Integer> numo = new ArrayList<>();
numo.add(1);
numo.add(2);
for(int i=0;i<numo.size() ;i++) {
    System.out.println(numo.get(i));
}

When we initialize an ArrayList the size of Array List would be 0 initially. That's why the code

for(int i=0;i<numo.size() ;i++)  {
   numo.add(i);
}

when iterating with numo.size() prints nothing.

ARRAY LIST JAVA

1.What is an ArrayList?

The array list is one which contains an array buffer within it into which the elements of the ArrayList are stored.The capacity of the ArrayList is the length of this array buffer.

2. What happens when i do not provide any size during array list intializtion?

Any empty ArrayList will be expanded to DEFAULT_CAPACITY when the first element is added.

DEFAULT_CAPACITY value of an array list is 10

3.What happens during the following intialization?

ArrayList<Integer> numo = new ArrayList<Integer>();

This would invoke array list constructor which would be like

private static final Object[] EMPTY_ELEMENTDATA = {};

/**
 * This would construct an empty list with an initial capacity of ten.
 */
public ArrayList() {
    super();
    this.elementData = EMPTY_ELEMENTDATA;
}

3.How my values are inserted into array list?

i] This checks for capacity of internal array and adds the element into the array buffer within it.

/*
Appends the specified element to the end of this list
e - element to be appended to this list
returns - true if element is added successfully - false otherwise 
*/
public boolean add(E e) {
    ensureCapacityInternal(size + 1);   //Increments modCount!!
    elementData[size++] = e;
    return true;
}

4.How does the array capacity (array here refers to the object array within the array list) is increased internally?

i] Ensuriy capacity

private void ensureCapacityInternal(int minCapacity) {
    if (elementData == EMPTY_ELEMENTDATA) {
        minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
    }

    ensureExplicitCapacity(minCapacity);
}

private void ensureExplicitCapacity(int minCapacity) {
    modCount++;

    // overflow-conscious code
    if (minCapacity - elementData.length > 0)
        grow(minCapacity);
}

ii] If the capacity of the array is not enough to hold the new element - then the size of the array is grown.

/**
 * Increases the capacity to ensure that it can hold at least the
 * number of elements specified by the minimum capacity argument.
 *
 * @param minCapacity the desired minimum capacity
 */
private void grow(int minCapacity) {
    // overflow-conscious code
    int oldCapacity = elementData.length;
    int newCapacity = oldCapacity + (oldCapacity >> 1);
    if (newCapacity - minCapacity < 0)
        newCapacity = minCapacity;
    if (newCapacity - MAX_ARRAY_SIZE > 0)
        newCapacity = hugeCapacity(minCapacity);
    // minCapacity is usually close to size, so this is a win:
    elementData = Arrays.copyOf(elementData, newCapacity);
}

5. Is my ArrayList overflow-conscious ?

Yes

6. When does an ArrayList throws OutOfMemoryError() ?

When requested array size exceeds VM limit

7. What is the maximum size limit of an ArrayList?

The maximum size of array to allocate is (Integer.MAX_VALUE - 8) ie; (2 ^31 - 9)

  • Some VMs reserve some header words in an array. Attempts to allocate larger arrays may result in OutOfMemoryError: Requested array size exceeds VM limit.

Here is the source code of ArrayList.java

Your numo list is empty. Try adding some values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM