java basis of an array of 1

array:
God horse to learn an array?
- the code can be simplified variable defined
concepts array: the nature of the data container is located, for storing a set of consecutive identical data type container.
Format:
data type data variable [] = new amount Name Data Type [length of the array]
Java stack memory, said subsequent ~
Features:
Eight basic data types, data types may be referenced string
satisfies naming variables, are generally complex, such as: scores
itself is a reference type
to meet an array of variables three elements: the
declaration of the array: tell the computer to open up a corresponding plurality of contiguous address space data type of
array-valued: to initialize each element in the array
using the array: through the array name to access the data
through index "index" to access elements in the array: the array name [index]
index starts from 0 [~ 0 the length of the array -1] range of
each element of the array must be the same type of data
as follows:
public class ArrayDemos {
public static void main (String [] args) {
int [] Array = new new int [. 5];
System.out.println (Array [. 6]);
Array [2] = 2;
Array [. 3] = 2;
the System .out.println (array [1]);
System.out.println(array[2]);
System.out.println(array[3]);
System.out.println(array[4]);
System.out.println(array[5]);
}
}

With the following error:
Exception in the Thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
AT come.sxt.arr.ArrayDemos.main (ArrayDemos.java:7)
Analysis: The array bounds
the causes: Visit the index does not exist
Solution: Check the an array index is within a range of 0 to -1 length of the array
if arr = null;
appeared in the null pointer exception
causes: the type of a variable reference address value did not give
solutions: identify the type of error related to the reference region check whether to create a new object [].
Initialize the array elements:
static initialization: initializing the assignment for each element while not necessary to specify the length of the array, the system will automatically calculate the length of the array according to the number of elements
such as: Data Type [] = {11, 22 data variable name , 33,44,55,66};
data type [] = new array variable name data type [] {11,22,33,44,55,66};
dynamic initialization: initialize the same time, the system gives each element given a default value, the length of the array must be specified, the default value of characteristics
such as: byte short int long default value 0
a float Double default value is 0.0
char default '\ u0000' null character is a
boolean false default
reference type default value is null
iterate:
tools: arrays.
如:int[] arr = { 11, 22, 33, 44, 55, 66,77 };
//System.out.println(arrayToString(arr));
System.out.println(Arrays.toString(arr));

Guess you like

Origin blog.csdn.net/qq_33570145/article/details/94644680