ArrayList 空指针异常
ArrayList nullPointerException
我在向 hello
ArrayList
添加对象时收到 NullPointerException
。我想在特定索引处添加对象,所以如果我不事先将 null
个对象添加到数组中,当我尝试添加一个之前没有索引的索引时,我会得到 IndexOutOfBoundsException
人口还没有。为什么我得到 NullPointerException
并且有任何其他方法可以实现它吗?
public void test()
{
ArrayList<TEST> temp = new ArrayList<>(4);
temp.add(0,new TEST(2));
temp.add(1,new TEST(3));
temp.add(2,new TEST(1));
temp.add(3,new TEST(0));
for(int i=0; i<4; i++)
Log.e("test", "i: "+i+ " index: "+temp.get(i).x);
ArrayList<TEST> hello = new ArrayList<>(4);
hello.add(null);
hello.add(null);
hello.add(null);
hello.add(null);
hello.add(temp.get(0).x, temp.get(0));
hello.add(temp.get(1).x, temp.get(1));
hello.add(temp.get(2).x, temp.get(2));
hello.add(temp.get(3).x, temp.get(3));
Log.e("test", "___________________________");
for(int i=0; i<4; i++)
Log.e("test", "i: "+i+ " index: "+hello.get(i).x);
}
public class TEST
{
int x;
public TEST(int x) {
this.x = x;
}
}
写的时候
hello.add(temp.get(0).x, temp.get(0));
您没有替换 null
放在 temp.get(0).x
索引中。您只需将 null
移动到下一个索引。
因此,在循环中:
for(int i=0; i<4; i++)
Log.e("test", "i: "+i+ " index: "+hello.get(i).x);
你遇到一个空值,所以 hello.get(i).x
抛出 NullPointerException
。
改为
hello.set(temp.get(0).x, temp.get(0));
hello.set(temp.get(1).x, temp.get(1));
hello.set(temp.get(2).x, temp.get(2));
hello.set(temp.get(3).x, temp.get(3));
为了用非空值替换所有空值。
您可以覆盖 ArrayList.set()
方法以在特定索引处添加对象:
public class SparseArrayList<E> extends ArrayList<E> {
@Override
public E set(int index, E element) {
while (size() <= index) add(null);
return super.set(index, element);
}
}
我认为您收到错误是因为您尝试向列表中添加超过 4 个元素,该列表的首字母为 4 个最大容量。
我在向 hello
ArrayList
添加对象时收到 NullPointerException
。我想在特定索引处添加对象,所以如果我不事先将 null
个对象添加到数组中,当我尝试添加一个之前没有索引的索引时,我会得到 IndexOutOfBoundsException
人口还没有。为什么我得到 NullPointerException
并且有任何其他方法可以实现它吗?
public void test()
{
ArrayList<TEST> temp = new ArrayList<>(4);
temp.add(0,new TEST(2));
temp.add(1,new TEST(3));
temp.add(2,new TEST(1));
temp.add(3,new TEST(0));
for(int i=0; i<4; i++)
Log.e("test", "i: "+i+ " index: "+temp.get(i).x);
ArrayList<TEST> hello = new ArrayList<>(4);
hello.add(null);
hello.add(null);
hello.add(null);
hello.add(null);
hello.add(temp.get(0).x, temp.get(0));
hello.add(temp.get(1).x, temp.get(1));
hello.add(temp.get(2).x, temp.get(2));
hello.add(temp.get(3).x, temp.get(3));
Log.e("test", "___________________________");
for(int i=0; i<4; i++)
Log.e("test", "i: "+i+ " index: "+hello.get(i).x);
}
public class TEST
{
int x;
public TEST(int x) {
this.x = x;
}
}
写的时候
hello.add(temp.get(0).x, temp.get(0));
您没有替换 null
放在 temp.get(0).x
索引中。您只需将 null
移动到下一个索引。
因此,在循环中:
for(int i=0; i<4; i++)
Log.e("test", "i: "+i+ " index: "+hello.get(i).x);
你遇到一个空值,所以 hello.get(i).x
抛出 NullPointerException
。
改为
hello.set(temp.get(0).x, temp.get(0));
hello.set(temp.get(1).x, temp.get(1));
hello.set(temp.get(2).x, temp.get(2));
hello.set(temp.get(3).x, temp.get(3));
为了用非空值替换所有空值。
您可以覆盖 ArrayList.set()
方法以在特定索引处添加对象:
public class SparseArrayList<E> extends ArrayList<E> {
@Override
public E set(int index, E element) {
while (size() <= index) add(null);
return super.set(index, element);
}
}
我认为您收到错误是因为您尝试向列表中添加超过 4 个元素,该列表的首字母为 4 个最大容量。