如果程序需要处理多个相同数据类型的值,可以为每个值声明一个变量。例如,显示彩票号码的程序:...
如果程序需要处理多个相同数据类型的值,可以为每个值声明一个变量。例如,显示彩票号码的程序:
int lotteryNumber1 = 16; int lotteryNumber2 = 32; int lotteryNumber3 = 12; int lotteryNumber4 = 23; int lotteryNumber5 = 33;
处理可以分组在一起的值的更优雅的方法是使用数组。数组是一个容器,它包含固定数量的数据类型值。在上面的示例中,彩票号码可以组合在一个整数数组中:
int[] lotteryNumbers = {16,32,12,23,33,20};
将数组视为一行框。数组中的框数不能更改。每个框可以保存一个值,只要它与其他框中包含的值具有相同的数据类型。您可以查看框内的值,或者用另一个值替换框中的内容。当谈到数组时,这些框称为元素。
声明和初始化数组
数组的声明语句类似于用于声明任何其他变量的声明语句。它包含后跟数组名称的数据类型-唯一的区别是在数据类型旁边包含方括号:
int[] intArray; float[] floatArray;
上面的声明语句告诉编译器
intArrayvariable is an array of ints, floatArrayis an array of floatsand charArray intArray = new int[10]; The number inside the brackets defines how many elements the array holds. The above assignment statement creates an int array with ten elements. Of course, there's no reason why the declaration and assignment can't happen in one statement: float[] floatArray = new float[10]; Arrays are not limited to primitive data types. Arrays of objects can be created: String[] names = new String[5];
Using an Array
Once an array has been initialized the elements can have values assigned to them by using the array's index. The index defines the position of each element in the array. The first element is at 0, the second element at 1 and so on. It's important to note that the index of the first element is 0. It's easy to think that because an array has ten elements that the index is from 1 to 10 instead of from 0 to 9. For example, if we go back to the lottery numbers example we can create an array containing 6 elements and assign the lottery numbers to the elements: int[] lotteryNumbers = new int[6]; lotteryNumbers[0] = 16; lotteryNumbers[1] = 32; lotteryNumbers[2] = 12; lotteryNumbers[3] = 23; lotteryNumbers[4] = 33; There is a shortcut to filling elements in an array by putting the values for the elements in the declaration statement: int[] lotteryNumbers = {16,32,12,23,33,20}; The values for each element is placed inside a pair of curly brackets. The order of the values determines which element is assigned the value starting with index position 0. The number of elements in the array is determined by the number of values inside the curly brackets. To get the value of an element its index is used: System.out.println("The value of the first element is " + lotteryNumbers[0]); To find out how many elements an array has use the length field: System.out.println("The lotteryNumbers array has " + lotteryNumbers.length + " elements");
Note: A common mistake when using the length method is to forget is to use the length value as an index position. This will always result in an error as the index positions of an array are 0 to length - 1.
Multidimensional Arrays
The arrays we have been looking at so far are known as one-dimensional (or single dimensional) arrays. This means they only have one row of elements. However, arrays can have more than one dimension. A multidimensional is actually an array that contains arrays: int[][] lotteryNumbers = {{16,32,12,23,33,20},{34,40,3,11,33,24}}; The index for a multidimensional array consists of two numbers: System.out.println("The value of element 1,4 is " + lotteryNumbers[1][4]); Although the length of the arrays contained within a multidimensional array do not have to be the same length: String[][] names = new String[5][7];
Copying an Array
To copy an array the easiest way is to use the arraycopy method of the System class. The arraycopy method can be used to copy all the elements of an array or a subsection of them. There are five parameters passed to the arraycopy public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) For example, to create a new array containing the last four elements of an int int[] lotteryNumbers = {16,32,12,23,33,20}; int[] newArrayNumbers = new int[4]; As arrays are a fixed length the arraycopy To further your knowledge about arrays you can learn about manipulating arrays using the Arrays class and making dynamic arrays (i.e., arrays when the number of elements is not a fixed number) using the ArrayList class.