Java study notes a (one-dimensional array)

Array (Array)

1, be understood that the array : the array is a plurality of the same type of data arrangement according to a certain order of collection, using a name, and by way of unified management number.

arr = [2,3,45,67,8,9,0,2]

2, an array of related concepts :

  • 1, the array name
  • 2, the elements of the array
  • 3, the subscript (subscript index)
  • 4, the length of the array

3, an array of features:

  • 1, the array is ordered
  • 2, the basic elements of the array can also be a data type reference data types;
  • 3, the length of the array can not be changed once it is determined;
  • 4, create an array of objects would open up a block of contiguous space in memory

4, the classification of the array : array dimension can be classified according to the one-dimensional arrays, two-dimensional arrays and so on. The data type of internal elements: an array of reference data types and data types of the basic array.
5, one-dimensional array of use :

  • 1, declare and initialize the array;
  • 2, the specified position in the array of reference elements;
  • 3, the length of the array;
  • 4, through the array;
  • 5, the array default initialization value;
  • 6, the memory array resolved;

Here Insert Picture Description

6, the default initialization values array elements:

  • Shaping the array elements are: 0;
  • Array element is a float: 0.0
  • Char array element type is: 0 or '\ u0000', rather than '0'
  • Array element is a boolean: false
  • Array elements are reference data types: null

Code:

public class ArrayTest {
	
	public static void main(String[] args) {
		
		//1. 一维数组的声明和初始化
		int num;//声明
		num = 10;//初始化
		int id = 1001;//声明 + 初始化
		
		int[] ids;//声明
	    //1.1 静态初始化:数组的初始化和数组元素的赋值操作同时进行
		ids = new int[]{1001,1002,1003,1004};
		//1.2动态初始化:数组的初始化和数组元素的赋值操作分开进行
		String[] names = new String[5];
		
		//错误的写法:
//		int[] arr1 = new int[];
//		int[5] arr2 = new int[5];
//		int[] arr3 = new int[3]{1,2,3};
		
		//也是正确的写法:
		int[] arr4 = {1,2,3,4,5};//类型推断
		
		//总结:数组一旦初始化完成,其长度就确定了。
		
		//2.如何调用数组的指定位置的元素:通过角标的方式调用。
		//数组的角标(或索引)从0开始的,到数组的长度-1结束。
		names[0] = "王铭";
		names[1] = "王赫";
		names[2] = "张学良";
		names[3] = "孙居龙";
		names[4] = "王宏志";//charAt(0)
//		names[5] = "周扬";
		
		//3.如何获取数组的长度。
		//属性:length
		System.out.println(names.length);//5
		System.out.println(ids.length);
		
		//4.如何遍历数组
		/*System.out.println(names[0]);
		System.out.println(names[1]);
		System.out.println(names[2]);
		System.out.println(names[3]);
		System.out.println(names[4]);*/
		
		for(int i = 0;i < names.length;i++){
			System.out.println(names[i]);
		}
		
		
	}

}
Published 11 original articles · won praise 14 · views 5459

Guess you like

Origin blog.csdn.net/ssnszds/article/details/104292949