java Java数组创建
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3779299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java array creation
提问by Matthieu Napoli
This is just a simple question, and I can't find the answer in the documentation !
这只是一个简单的问题,我在文档中找不到答案!
String args[] = new String[0];
args[0] = "test";
Is that correct ? Does this creates an array with 1 element or 0 elements ?
那是对的吗 ?这是否会创建一个包含 1 个元素或 0 个元素的数组?
Thank you, I know, stupid question, but I couldn't find the answer in the Java doc.
谢谢,我知道,愚蠢的问题,但我在 Java 文档中找不到答案。
回答by Gadolin
String[] arr = new String[]{"test"}
String[] arr = new String[]{"test"}
回答by Thirler
This creates an array with length 0. The second line will give an ArrayIndexOutOfBoundsExpection
.
这将创建一个长度为 0 的数组。第二行将给出一个ArrayIndexOutOfBoundsExpection
.
回答by nanda
Your code is wrong. The first number states the length of the array, so it should be 1
String args[] = new String[1];
你的代码是错误的。第一个数字表示数组的长度,所以它应该是 1
String args[] = new String[1];
The first element in an array is labeled as myArray[0]
数组中的第一个元素标记为 myArray[0]
回答by Matthijs Bierman
If you're trying to do the PHP-like equivalent of args[] = "new entry" then take a look at
如果您正在尝试执行类似 PHP 的 args[] = "new entry" 等效项,请查看
List<String> args = new ArrayList<String>();
args.add("test");
args.add("and some more");
args.add("and even more");
This works fine, and will expand your List automatically. When you need to convert it to an array, you can use:
这很好用,并且会自动扩展您的列表。当需要将其转换为数组时,可以使用:
String[] argArray = args.toArray(new String[args.size()]);
回答by BigMac66
Yep it does seem a little odd - when you create the array you are declaring how many elements the array will have so 0 means no elements. Yet when you traverse an array the first element is the 0th element not the 1st element... Just remember that size/length are not the same as index.
是的,它看起来确实有点奇怪——当你创建数组时,你是在声明数组将有多少个元素,所以 0 意味着没有元素。然而,当你遍历一个数组时,第一个元素是第 0 个元素而不是第一个元素......请记住,大小/长度与索引不同。
回答by sje397
An array of length 5 is created with:
创建长度为 5 的数组:
String myArray[] = new String[5];
The items in this array are indexed using 0, 1, 2, 3, 4 - note that they start at index 0, not index 1, and so go up to (array length - 1).
此数组中的项目使用 0、1、2、3、4 进行索引 - 请注意,它们从索引 0 开始,而不是从索引 1 开始,因此一直到(数组长度 - 1)。
So
所以
new String[0]
creates an array of length 0. Assigning to index 0 will cause an error - there are no positions in the array to assign to.
创建一个长度为 0 的数组。分配给索引 0 将导致错误 - 数组中没有要分配的位置。
new String[1]
would create an array of length 1, with a single position at index 0, so you could then legally do:
将创建一个长度为 1 的数组,在索引 0 处有一个位置,因此您可以合法地执行以下操作:
myArray[0] = "happy days";
回答by Bert F
String args[] = new String[0];
This creates an array with no elements. Accessing any element, even args[0]
, would cause an ArrayIndexOutOfBoundsException
. The number of components of the array is available in args.length
.
这将创建一个没有元素的数组。访问任何元素,甚至args[0]
,都会导致ArrayIndexOutOfBoundsException
. 数组的组件数量在args.length
.
String args[] = new String[1];
This creates an array with 1 element. The element is accessed as args[0]
. The first element is always at index 0. Accessing any other element would cause an ArrayIndexOutOfBoundsException
.
这将创建一个包含 1 个元素的数组。该元素作为 访问args[0]
。第一个元素总是在索引 0 处。访问任何其他元素都会导致ArrayIndexOutOfBoundsException
.
String args[] = new String[10];
This creates an array with 10 elements. First element is args[0]
and the last element is args[9]
. The last position is always one less than the size of the array.
这将创建一个包含 10 个元素的数组。第一个元素是args[0]
,最后一个元素是args[9]
。最后一个位置总是比数组的大小小一个。
References:
参考:
Java tutorial on arrays: http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Spec: http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
An array object contains a number of variables. The number of variables may be zero, in which case the array is said to be empty. The variables contained in an array have no names; instead they are referenced by array access expressions that use nonnegative integer index values. These variables are called the components of the array. If an array has n components, we say n is the length of the array; the components of the array are referenced using integer indices from 0 to n - 1, inclusive.
关于数组的 Java 教程:http: //download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
规范:http: //java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
一个数组对象包含许多变量。变量的数量可能为零,在这种情况下,数组被称为空。数组中包含的变量没有名称;相反,它们由使用非负整数索引值的数组访问表达式引用。这些变量称为数组的组件。如果一个数组有 n 个分量,我们说 n 是数组的长度;使用从 0 到 n - 1(含)的整数索引来引用数组的组件。
回答by foo
new String[x]
will create an empty array of Strings with size x.
With x=0, your Array will have noentries, so any attempt to access its elements will result in an exception.
If you want it to have oneelement, you should specify you want oneelement: new String[1]
will create an Array of Strings with 1 entry.
new String[x]
将创建一个大小为 x 的空字符串数组。x=0 时,您的 Array 将没有条目,因此任何访问其元素的尝试都将导致异常。如果你希望它有一个元素,你应该指定你想要一个元素:new String[1]
将创建一个包含 1 个条目的字符串数组。
While the above parameter specifies the sizeof the array, the one you use later is the index. In many languages, [] are used for both index (in regular use) and size (when creating arrays), which may be confusing.
虽然上述参数指定了数组的大小,但稍后使用的是index。在许多语言中,[] 用于索引(经常使用)和大小(创建数组时),这可能会造成混淆。
Simple rule: a valid index will always be >= 0, and < size of the array.
简单规则:有效索引总是 >= 0,并且 < 数组的大小。
0 <= index < size
0 <= index < size
An index, also called offset, is how far from the start you go - how many elements into the array you step.
索引,也称为偏移量,是指距您开始的位置有多远 - 您步进的数组中有多少元素。
回答by codaddict
String args[] = new String[0];
String args[] = new String[0];
Creates an array of size 0
also called as an emptyarray. Since the array contains no elements, no index can be used on it including 0
. Usage of anyindex on it leads to java.lang.ArrayIndexOutOfBoundsException
.
创建一个大小的数组,0
也称为空数组。由于数组不包含任何元素,因此不能对其使用任何索引,包括0
. 在其上使用任何索引都会导致java.lang.ArrayIndexOutOfBoundsException
.