为什么我的以下程序不打印任何输出(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));
            }
    
        }       
}

代码正在运行,但是当我运行时不会打印任何内容。我想打印阵列列表中的所有整数。我是Java的新手。

您可以尝试在numo列表中添加一些值,然后循环以打印这些值

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

您的list最初大小为零,因此您无法迭代list并打印任何内容。

不要尝试迭代并将项目添加到同一列表(没有适当条件以终止循环)它将生成Infinite Loop

因此,首先将元素添加到列表中,然后迭代它,如下所示:

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

初始化ArrayList数组列表的大小最初为0时。这就是为什么代码

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

numo.size()迭代时什么都没有打印。

数组列表Java

1.什么是arraylist?

数组列表是其中包含一个数组缓冲区,其中存储了阵列列表的元素。阵列列表的容量是此数组缓冲区的长度。

2。当我在数组列表Intializtion期间不提供任何尺寸时会发生什么?

添加第一个元素时,任何空阵列列表都将扩展到DEFAULT_CAPACITY

DEFAULT_CAPACITY value of an array list is 10

3.以下Intialization期间会发生什么?

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

这将调用数组列表构造函数,就像

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.我的值如何插入数组列表?

i]这检查了内部数组的容量,并将元素添加到其中的数组缓冲区中。

/*
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.数组容量如何(这里的数组引用数组列表中的对象数组)在内部增加?

i] nesuriy容量

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]如果数组的容量不足以容纳新元素 - 那么数组的大小就会生长。

/**
 * 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。我的arraylist溢出意识是否符合?

6。ArrayList什么时候抛出OutOfMemoryError()

当要求的数组大小超过VM限制

7。阵列列表的最大尺寸限制是多少?

分配的数组的最大大小为(integer.max_value -8)IE;(2 ^31-9)

  • 一些VM在数组中保留一些标题单词。尝试分配较大阵列的尝试可能会导致超eMemoryError:请求的数组大小超过VM限制。

这是arraylist.java的源代码

您的numo列表为空。尝试添加一些值

最新更新