您可以通过执行设置ArrayList的初始大小
ArrayList<Integer> arr=new ArrayList<Integer>(10);
Run Code Online (Sandbox Code Playgroud)
但是,你做不到
arr.add(5, 10);
Run Code Online (Sandbox Code Playgroud)
因为它会导致越界异常.
如果您无法访问分配的空间,设置初始大小有什么用?
add函数定义为add(int index, Object element)我没有添加到索引10.
当我做
ArrayList<Integer> arr = new ArrayList<Integer>(10);
arr.set(0, 1);
Run Code Online (Sandbox Code Playgroud)
Java给了我
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.set(Unknown Source)
at HelloWorld.main(HelloWorld.java:13)
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法可以预先保留ArrayList的大小,然后立即使用索引,就像数组一样?
参数(int initialCapacity)中的含义是什么ArrayList,我认为它是元素的数量,但是当我这样做时它不起作用:
public class MyClass {
private ArrayList<Integer> arr;
public MyClass(int n_elements) {
arr = new ArrayList<Integer>(n_elements);
}
}
Run Code Online (Sandbox Code Playgroud) 在处理时ArrayList,我发现在使用构造函数设置了数组的初始大小之后initialCapacity,set()尽管创建了数组,但是使用将抛出异常,但是未正确设置大小.
使用ensureCapacity()将无法工作,因为它基于elementData数组而不是size.
还有其他副作用静态的,因为DEFAULT_CAPACITY用ensureCapacity().
使这项工作的唯一方法是在使用构造函数后使用add()尽可能多的时间.
请检查以下代码.
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
List test = new ArrayList(10);
test.set(5, "test");
System.out.println(test.size());
}
Run Code Online (Sandbox Code Playgroud)
我不确定为什么java会抛出这个异常.
我期望的行为:test.size()应该返回10并且设置(5,...)应该工作.
实际:抛出异常IndexOutOfBoundsException.
那么导致问题的set方法是什么?
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 175, Size: 175
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.remove(Unknown Source)
at GetData.main(GetData.java:42)
Run Code Online (Sandbox Code Playgroud)
这是我列入我的列表的例外l,但是当我打电话时l.size(),它返回188!
这是一个SSCCE(嗯,尽可能简洁):
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class GetData {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
//optimize();
BufferedReader r = new BufferedReader(new FileReader(new File("countrydata - optimized.txt")));
String str = "";
String line = null;
int x = 0;
while ((line = r.readLine()) != null) {
str += line;
x ++;
if (x % 20 …Run Code Online (Sandbox Code Playgroud) 这是我目前在项目上取得的进展.我试图实现这个ArrayList的东西,但文件继续trow相同的异常.
import java.util.*;
public class Numerican{
public static void main( String [] args ){
Scanner input = new Scanner(System.in);
ArrayList<Integer> array = new ArrayList<Integer>(10);
int count = input.nextInt() * 2;
while (count > 0){
array.add( 0 );
count = count - 1;
array.add(count, 2);
}
array.add(2, input.nextInt());
System.out.println(array.get(2));
}
}
Run Code Online (Sandbox Code Playgroud)
我的理解是= new ArrayList<Integer>(10);将数组大小设置为10.我做错了吗?
我有一个最奇怪的问题,可能有一个简单的解决方案。
我创建并初始化了一个列表,然后继续创建列表类型的 4 个对象。在这些的构造函数中,它们将自己放在列表中。或者至少应该这样做。我总是得到一个越界异常,我不知道为什么。我将列表的大小设置为 402(对于所有可能的 VK 值),但在控制台和调试中,它总是说它的大小为 0,无论我设置它有多大或多空......
public class InputHandler implements KeyListener
{
public static List<Key> keyList = new ArrayList<Key>(KeyEvent.KEY_LAST);
public Key up = new Key(KeyEvent.VK_UP);
public Key down = new Key(KeyEvent.VK_DOWN);
public Key left = new Key(KeyEvent.VK_LEFT);
public Key right = new Key(KeyEvent.VK_RIGHT);
public class Key
{
public int keyCode;
public Key(int defaultCode)
{
this.keyCode = defaultCode;
keyList.add(keyCode,this);
}
public Key reMapKey(int newKey)
{
keyList.remove(keyCode);
keyList.set(newKey, this);
this.keyCode = newKey;
return this;
}
}
}
Run Code Online (Sandbox Code Playgroud)
代码还有更多内容,但我尝试 SSCCE 它。 …