JAVA array base -----

Array


  • Array: aFixed-length, Including theThe same type Data container
  • This data array is a reference type, the type of memory storage array may be substantially types may be used type

Array definition (statement)

  • int [] a; declares an array variable.
  • [] Indicates that the variable is an array
  • int represents each element in the array is an integer
  • a variable name
    • However, this is just a statement, not create arrays

Sometimes written as int a []; there is no difference, what you see is pleasing to the eye problem

public class HelloWorld {
	public static void main(String[] args) {
		// 声明一个数组
		int[] a;
	}
}

Array-valued (initialization)

  • Static initialization (with the length of all elements)
public class HelloWorld {
	public static void main(String[] args) {
        int[] arry = new int[] {10,20,30,40,50};
        int[] arry ={10,20,30,40,50}
    }
  • Dynamic initialization (having a length no element -> default)
int[]array -new int[5]

Access array elements

  • By elements in the array to access location
  • Location ----> index index
  • The index is in the range of
  • If the array index is outside the range of the array
  • Abnormal ArryIndexOutOfBoundsException there will be a run-time
public class HelloWorld {
	public static void main(String[] args) {
        int[] arry = new int[] {10,20,30,40,50};
        int[] arry ={10,20,30,40,50}
        //通过元素在数组中的位置index(索引 下标)
        //array[index]
        //从数组内取得某一个位置的元素
        int value = array[0];
        System.out.println(value);
        //向数组内的某一个位置存入元素
        array[4]=400;
        
        
        //索引是有范围的
        int value=array[5];
        System.out.println(value);
        //异常----运行时异常
        //ArryIndexOutOfBoundsException 数组索引越界
    }
}    

Through the array elements

  • To access the array cycle through each element

  • for(;;){

}

//将数组中的每一个元素都拿出来看一看
for(int index=0; i<5; index++){
    int value =array[index];
    System.out.println(value);
}
  • After JDK1.5 new features enhanced for loop

  • for (variable (accept every element within the data) own definition: iterated array array) {

}

for(int value:array){
    System.out.println(value);
}

  • The difference between the two for
  1. Normal for there are three elements Index index to find the location of a particular element, you can save a certain position value and direct access to an array index value can be
  2. Enhanced for not only the value of stored value

Basic data types and data type differences in structure memory references

  • 所有的变量空间都存储在栈内存
    
  • 变量空间可以存储基本数据类型 也可以存储引用数据类型
    
  • 如果变量空间存储的是基本数据类型 存储的是值 一个变量的值改变 另一个不会跟着改变
    
  • 如果变量空间存储的是引用数据类型 存储的是引用(地址) 一个变量地址对应的值改变 另一个跟着改变

Guess you like

Origin www.cnblogs.com/CGGG/p/12549764.html