This is a simple question and I cannot find the answer in the documentation!
String args[] = new String[0]; args[0] = "test";
It is right? Does this create an array with 1 element or 0 elements?
Thanks, I know, a stupid question, but I could not find the answer in the Java document.
This creates an array with a length of 0. The second line will give ArrayIndexOutOfBoundsExpection.
ArrayIndexOutOfBoundsExpection
String[] arr = new String[]{"test"}
. , 1String args[] = new String[1];
. , 1
String args[] = new String[1];
myArray [0]
PHP args [] = "new entry",
List<String> args = new ArrayList<String>(); args.add("test"); args.add("and some more"); args.add("and even more");
. , :
String[] argArray = args.toArray(new String[args.size()]);
, - , , , 0 . , 0- , 1- ... , / .
5 :
String myArray[] = new String[5];
0, 1, 2, 3, 4 - , 0, 1, ( - 1).
,
new String[0]
0. 0 - .
new String[1]
1 0, :
myArray[0] = "happy days";
String args[] = new String[0];
. , args[0], ArrayIndexOutOfBoundsException. args.length.
args[0]
ArrayIndexOutOfBoundsException
args.length
1 . args[0]. 0. ArrayIndexOutOfBoundsException.
String args[] = new String[10];
10 . args[0], args[9]. .
args[9]
:
Java : http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Spec: http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
. , . , , ; , . . n , , n - ; 0 n - 1 .
new String[x] x. x = 0, , . , , , : new String[1] 1 .
new String[x]
, , , index. [] ( ), ( ), .
: >= 0, < .
0 <= index < size
, , , - .
Creates a size array 0, also called an empty array. Since the array contains no elements, you cannot use an index on it, including 0. Using any index on it results in java.lang.ArrayIndexOutOfBoundsException.
0
java.lang.ArrayIndexOutOfBoundsException
here is a simple and official example .
Source: https://habr.com/ru/post/1766229/More articles:C # Reflection How-to? - reflectionHow to get the vertices of a polygon described by planes - c ++is there a class history visualization tool for SVN - version-controlExtjs apps on iphone - iosКак мне нужно изменить мое приложение WinForms для запуска в консоли? - c#Как напечатать положительное число как отрицательное число с помощью printf - cEntity Framework - add only foreign key values to a union - entity-frameworkHow to add .pdf extension when sending attachments from Android? - androidJQuery UI auto-check - jqueryPHP A simple way to replace or remove blank lines with str_replace - phpAll Articles