本章内容
声明数组变量
创建数组
处理数组
For-Each循环
多维数组
Arrays类
前言
数组对于每一门编程语言来说都是最重要的数据结构之一
Java语言中提供数组的目的:
用来存储固定大小的同类型元素
三个部分:
数组的声明、创建和初始化、给出其对于的代码
声明数组变量
语法:
dataType[] arrayRefVar; //首选的方法
dataType arrayRefVar[]; //效果相同,但不是首选方法
注意:
建议使用 dataType[] arrayRefVar 的声明风格声明数组变量。 dataType arrayRefVar[] 风格是来自 C/C++ 语言
实例:
double[] myList; //首选方法
double myList[]; //效果相同,不是首选方法
创建数组
语法:
arrayRefVar = new dataType[arraySize];
-
dataType[arraySize]创建了一个数组
-
把新创建的数组的引用赋值给变量arrayRefVar
数组变量的声明、创建数组可以用一条语句完成:
dataType[] arrayRefVar = new dataType[arraySize];
另一个方式:
dataType[] arrayRefVar = {value0,value1,...,valuen};
数组的元素是通过索引访问的。数组索引从 0 开始,所以索引值从 0 到 arrayRefVar.length-1
实例:
public class arrayLIst_Practice_code {
public static void main(String[] arguments) {
//数组大小
int size = 10;
//定义数组
double[] myList = new double[size];
myList[0] = 5.6;
myList[1] = 4.5;
myList[2] = 3.3;
myList[3] = 13.2;
myList[4] = 4.0;
myList[5] = 34.33;
myList[6] = 34.0;
myList[7] = 45.45;
myList[8] = 99.993;
myList[9] = 11123;
//计算所有元素的总和
double total = 0;
for (int i = 0; i < size; i++) {
total += myList[i];
}
System.out.print("总和为:" + total);
}
}
处理数组
特点:
数组的元素类型和数组的大小都是确定的,所以当处理数组元素时候,我们通常使用基本循环或者 For-Each 循环
实例:
-
数组的创建
-
数组的初始化
-
操纵数组
public class HandlearrayList_Practice_code {
public static void main(String[] arguments){
double[] myList = {1.9,2.9,3.4,3.5};
//打印所有数组元素
for(int i = 0; i < myList.length; i++){
System.out.print(myList[i] + "");
}
//计算所有元素的总和
double total = 0;
for(int i = 0; i < myList.length; i++){
total += myList[i];
}
System.out.println("Total is" + total);
//查找最大元素
double max = myList[0];
for(int i = 1; i < myList.length; i++){
if(myList[i] > max) max = myList[i];
}
System.out.println("Max is" + max);
}
}
For-Each循环
JDK1.5引进了一种新的数据类型,被称为For-Each循环或者加强型循环,它能在不适用下标的情况下遍历数组
for(type element:array)
{
System.out.println(element);
}
实例:
public class TestarrayList_Practice_code { public static void main(String[] arguments){ double[] myList = {1.9,2.9,3.4,3.5}; //打印所有数组元素 for( double element:myList ){ System.out.println(element); } } }
数组作为函数的参数
特点:
-
数组可以作为参数传递给方法
实例:
public class printArray_Practice_code { public static void printArray_Practice_code(int[] array){ for( int i = 0; i < array.length; i++){ System.out.print(array[i] + " "); } } } /* 调用printArray方法打印处结果 */ printArray(new int[]{3,1,2,6,4,2});
数组作为参数的返回值
public static int[] reverse(int[] list) { int[] result = new int[list.length]; for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; }
完整的方法是:
public class printArray { public static void printArray( int[] array ){ for( int i = 0; i < array.length; i++){ System.out.print(array[i] + " "); } } public static int[] reverse( int[] list ){ int[] result = new int[list.length]; for( int i = 0,j = result.length - 1; i < list.length; i++,j--){ result[j] = list[i]; } return result; } public static void main(String arguments[]){ //printArray(new int[]{3,1,2,4,5,6}); reverse(new int[]{3,2,1,4,5,6}); } } /* 一个类里面写了两个其他的方法然后最后在写一个main方法去调用上面写的方法 */