- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
考虑这个示例代码:
List<String> myList = new ArrayList<String>(7);
myList.add(5, "Hello");
myList.removeAll(Collections.singleton(null));
System.out.println(myList.size() + " objects:" );
for (String s : myList) {
System.out.println("\t" + s);
}
myList
以初始容量 7 进行初始化,然后下一行尝试在位置 5 处添加字符串“Hello”。这将引发 IndexOutOfBoundsException:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 0
我查看了this question关于 ArrayList 中“初始容量”的含义。我知道这个特定的构造函数正在为 7 个 String 元素分配空间,如果我们尝试将 8 个元素添加到列表中,它将不得不分配更多空间。
我不理解的是为什么它不创建一个大小为 7 的“空”列表,每个索引处都有空值,类似于我们声明 String[] myArray = new String[7]
时会发生的情况。 .我记得了解到 ArrayList 是 Java 对动态数组的实现,所以我期待类似的行为。如果我在声明 new ArrayList<String>(7)
时实际上没有分配 7 个字符串的空间,到底发生了什么?
最佳答案
What I don't understand is why it doesn't create an "empty" list of size 7, with null values at each index, similar to what would happen if we declared String[] myArray = new String[7].
这在某些情况下很有用……而在其他情况下则没有用。很多时候你有一个列表大小的上限你要创建(或者至少是一个猜测)但是你填充它......而你没有 em> 想要有一个大小错误的列表......所以你必须在“设置”值时维护一个索引,然后再设置大小。
I recall learning that ArrayList is Java's implementation of a dynamic array, so I'd expect a similar sort of behavior.
不,真的不是。这是一个可以调整大小的列表,并且在幕后使用一个数组。尽量不要把它想象成一个数组。
If I don't actually have space for 7 Strings allocated when I declare new
ArrayList<String>(7)
, what is actually happening?
你做有7个字符串引用的空间。 buffer 大小(即容量)至少为 7,但列表的 logical 大小仍为 0 - 您还没有向其中添加任何内容。这就像你有一张足够长 7 行的纸,但你还没有写任何东西。
如果您想要一个预填充列表,您可以轻松编写一个方法来创建一个:
public static List<T> createPrefilledList(int size, T item) {
ArrayList<T> list = new ArrayList<T>(size);
for (int i = 0; i < size; i++) {
list.add(item);
}
return list;
}
关于java - ArrayList 初始容量和 IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11908037/
将一个数组列表分配给另一个数组列表与在两个数组列表之间使用 addAll 方法有什么区别? 1 > arrayList = arrayList;//应该将后面的arrayList的值分配给第一个。 2
所以我在将 ArrayList 添加到我的 ArrayList 时遇到了一些问题。将其想象成一张 table 。 下面是一些示例代码: ArrayList currentRow = new Arra
我一直在尝试转换 ArrayList> to ArrayList> 的字符串 这是我尝试构建的代码。 public void convertString (ArrayList> templist) {
我有一个 ArrayList (alal) 和一个 ArrayList(al) 的 ArrayList。我想将 al 插入 alal,但我希望 alal.get(0) 包含 al 拥有的所有内容以及添
很抱歉标题太长,如果您能想到更好的标题,请告诉我! 我正在做的是尝试创建一个 ArrayList 的 ArrayList 并将 ArrayList 逐个添加到其中。两个AL>我所拥有的称为三角形和正方
我有带有值的 mList2。存在具有相同 id 的值。如何获取具有相同 id 的对象分组的 List 或 ArrayList 并将其添加到 ArrayList>? List mList2 = list
我正在创建一个 ArrayList的 ArrayList并添加 ArrayLists给它。但每次我对 ArrayList 进行更改时, 它反射(reflect)在 ArrayList 中. 示例: L
谁能解释一下ArrayList之间的区别是什么? , ArrayList和 ArrayList是什么时候使用它们?它们在实现层面上是相同的还是各自具有不同的含义? 最佳答案 ArrayList 特别是
这个问题在这里已经有了答案: Java generics: List> = new LinkedList>() is prohibited? (3 个答案) 关闭 9 年前。 为什么这段代码可以编译
我的 arraylistS 在覆盖数组列表中的行为类似于同一个实例。 我用其中一个来操作 i=0; manupulate((ArrayList)theCoveringRootArrayList.get
我们遇到这个错误 java.lang.NullPointerException at java.util.ArrayList.(Unknown Source) at de.mystuf
据我了解,ArrayList 类继承其父“List”类的 equals() 函数来查找两个成员对象是否相同。这是否意味着“contains()”线性搜索(使用“equal”)来查找 ArrayList
这个问题已经有答案了: What is the diamond operator in Java? (2 个回答) 已关闭 7 年前。 正如标题所说,在Java中,这两种语句有什么区别吗? 通常我都能
我正在尝试求解帕斯卡三角形。我有两个用 Java 编写的代码片段,第一个创建 inner ArrayList 几次并且对我来说效果很好。 但是在代码的第二个版本中,如果我修改 inner ArrayL
正如标题所示,我有两个 ArrayList。奇怪的是,在一个数组列表上设置一个值会改变另一个数组列表的值。 一些信息:这些是 Entry 类型的 ArrayList,每个列表都包含一个金额和一个值(这
我已经添加了一个项目到列表 a,然后添加了列表 a 到列表 b 并再次做了同样的事情。 我的问题是,如果我打印 b.get(0) 和 b.get(1),我会得到相同的列表,这两个项目都是 “一”和“二
我正在创建一个 ArrayList of ArrayList of ArrayList 的 ArrayList 并按以下方式填充它。它正确地填充它。我已经通过调试和 println 弄清楚了这一点。但
实现可以在 Arraylist 和 Integer 中存储任何级别的 ArrayList 的 ArrayList 的最佳方法是什么。 List> list = ArrayList(); 仅允许列表中最
在下面的示例中,我将如何将 ArrayList al4 的内容与其他 ArrayList 中的任何一个进行比较?以同样的方式,我将 al1 与 al2 进行了比较。 import java.util.
好的,所以我之前发布了一个线程,它回答了我的很多问题并帮助我改进了我的代码,但是,我遇到了另一个问题,我不知道为什么,但我认为也许该副本只是指向原始对象..(尽管我已尽力避免这种情况) 在我的游戏代码
我是一名优秀的程序员,十分优秀!