Java array first learning

Array writing specification in Java

In the C language, the definition of an array is often: int array[]; and the array specification in Java is: int[] array;

The value of the array is directly fixed


public class Test {
    
    
	public static void main(String[] args) {
    
    
		
		int[] array = {
    
    1,2,3};
		int i;
		
		for(i=0;i<array.length;i++){
    
    
			System.out.println("array[" +i + "]" + "=" +array[i]);
			
		}
		
	}
}

Not sure about the value of the array


public class Test {
    
    
	public static void main(String[] args) {
    
    
		
		int[] array = new int[3];
		int i;
		
		for(i=0;i<array.length;i++){
    
    
			array[i] = i;
		}
		
		for(i=0;i<array.length;i++){
    
    
			System.out.println("array[" +i + "]" + "=" +array[i]);
			
		}
		
	}
}

Another way of writing the second method


public class Test {
    
    
	public static void main(String[] args) {
    
    
		/*另一种写法,注意null是小写*/
		int array[] = null;
		array = new int[3];
		int i;
		
		for(i=0;i<array.length;i++){
    
    
			array[i] = i;
		}
		
		for(i=0;i<array.length;i++){
    
    
			System.out.println("array[" +i + "]" + "=" +array[i]);
			
		}
		
	}
}

Guess you like

Origin blog.csdn.net/zouchengzhi1021/article/details/113715907