|
public class Test7 {
//二维数组
//数组并不是等列数组
public static void main(String[] args){
int[][] demon3 = new int[][]{{1,3},{5,7,9},{2,4}};
//打印二维数组要使用双重循环
for(int i = 0;i< demon3.length;i++){
for(int j =0;j<demon3[i].length;j++){
System.out.print(demon3[i][j]+" ");
}
}
}
}
4.数组和方法的互操作
数组是引用数据类型,所有引用数据类型都可以为其设置多个栈内存所指向;所以在进行对数组的操作时,可以通过其他方式处理。
(1)方法接收数组
(2)方法返回数组
(3)方法修改数组
|
public class Test7 {
//数组与方法的互操作
public static void main(String[] args){
int[] demon = receive();
changeDouble(demon);
printArray(demon);//等价于 int[] temp =demon;
}
//方法接收数组组
public static int[] receive(){
return new int[]{1,27,18,21};//匿名数组
}
//方法返回数组
public static void printArray(int[] temp){
for(int i = 0;i < temp.length;i++){
System.out.println(temp[i]);
}
}
//方法改变数组
public static void changeDouble(int[] arr){
for(int j = 0;j<arr.length;j++){
arr[j] *=2;//每个数组元素扩大两倍
}
}
}
|
|
运行结果:2
54
36
42
|
内存分析图如下:

5.Java对数组的支持
1.对数组进行排序:
只要是基本数据类型的数组,sort方法都可以进行排序处理(升序处理)。
数组排序 : java.util.Arrays.sort(数组名称)
|
package test;
public class Test7 {
//java对数组的支持
public static void main(String[] args){
int[] demon5 = new int[]{20,8,4,92,1};
char[] demon6 = new char[]{\'c\',\'D\',\'a\',\'g\',\'A\'};
java.util.Arrays.sort(demon5);//对整型数组排序
java.util.Arrays.sort(demon6);//对字符型数组排序
printArray(demon5);
printArray(demon6);
}
public static void printArray(int[] temp){
for(int i = 0;i < temp.length;i++){
System.out.print(temp[i]);
System.out.print(" ");
}
}
public static void printArray(char[] temp){
for(int i = 0;i < temp.length;i++){
System.out.print(temp[i]);
System.out.print(" ");
}
}
}
|
运行结果:1 4 8 20 92 A D a c g |
2.实现数组的拷贝:
数组拷贝:指的是将一个数组的部分内容替换掉另一个数组的部分内容(必须是连续的)。
System.arraycopy(源数组名称,源数组开始点,目标数组名称,目标数组开始点,拷贝长度)
|
public class Test7 {
//实现数组的拷贝
//实现将demon2的2-4个元素拷贝至demon1的6-8个元素位置处
public static void main(String[] args){
int[] demon1 = new int[]{1,2,3,4,5,6,7,8,9};
int[] demon2 = new int[]{9,8,7,6,5,4,3,2,1};
System.arraycopy(demon2,1,demon1,5,3);
printArray(demon1);
}
public static void printArray(int[] temp){
for(int i = 0;i < temp.length;i++){
System.out.print(temp[i]);
System.out.print(" ");
}
}
}
|
6.数组案例
给出以下数组,要求可以统计出该数组的最大值、最小值、平均值、总和。
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
1 |
4 |
3 |
4 |
55 |
77 |
6 |
9 |
8 |
(1)简单的实现方法实现
|
public class Test7 {
public static void main(String[] args){
int[] demon = new int[]{1,4,3,4,55,77,6,9,8};
int max = demon[0];
int min = demon[0];
int sum = demon[0];
double avg = 0.0;
for(int i = 1;i< demon.length;i++){
sum += demon[i];
if(demon[i] > max){
max = demon[i];
}
if(demon[i] < min){
min = demon[i];
}
}
System.out.println("max= "+max);
System.out.println("min= "+min);
System.out.println("sum= "+sum);
System.out.println("avg= "+(1.0*sum/demon.length));
}
}
|
|
运行结果:
max= 77
min= 1
sum= 167
avg= 18.555555555555557
|
由上述代码可以看出:主函数中代码量大,代码比较凌乱;主方法相当于客户端调用,代码应越简单越越好。
(2)使用方法与数组互操作实现
|
//数组排序(数组与方法互操作)
public class Test7 {
public static void main(String[] args){
int[] demon = new int[]{1,4,3,4,55,77,6,9,8};
maxValue(demon);
minValue(demon);
sumAndAvg(demon);
}
//求最大值
public static void maxValue(int[] temp){
int max = temp[0];
for(int i = 1;i < temp.length;i++){
if(temp[i] > max){
max = temp[i];
}
}
System.out.println("max= "+max);
}
//求最小值
public static void minValue(int[] temp){
int min = temp[0];
for(int i = 1;i < temp.length;i++){
if(temp[i] < min){
min = temp[i];
}
}
System.out.println("min= "+min);
}
//求平均值和总和
public static void sumAndAvg(int[] temp){
int sum = temp[0] ;
for(int i = 1;i < temp.length;i++){
sum += temp[i];
}
double avg = 1.0*sum/temp.length;
System.out.println("sum= "+sum);
System.out.println("avg= "+avg);
}
}
|
|
运行结果:
max= 77
min= 1
sum= 167
avg= 18.555555555555557
|
使用如上方式使得逻辑比较清晰,主函数中代码量减少。
7.对象数组
可以看出前面所定义的都是基本数组,对象数组往往是以引用数据类型为主的定义;对象数组中保存的内容要比普通类型多;如:接口、类。
(1)对象数组的动态初始化
类名称[ ] 对象数组名 = new 类名称[长度] |
|
//对象数组的动态初始化
class Person{
private String name;
private int age;
public Person(String name, int age){//构造方法
this.name = name;
this.age = age;
}
public void getPrint(){
System.out.println("姓名: "+this.name+" 年龄: "+this.age);
}
}
public class Test7 {
public static void main(String[] args){
Person[] per = new Person[3];
per[0] = new Person("张三",16);
per[1] = new Person("李四",18);
per[2] = new Person("王五",20);
for(int i = 0;i < per.length;i++){
per[i].getPrint();
}
}
}
|
|
运行结果:
姓名: 张三 年龄: 16
姓名: 李四 年龄: 18
姓名: 王五 年龄: 20
|
(2)对象数组的静态初始化
类名称[ ] 对象数组名 = new 类名称[]{new 类名称()…… } |
|
class Person{
private String name;
private int age;
public Person(String name, int age){//构造方法
this.name = name;
this.age = age;
}
public void getPrint(){
System.out.println("姓名: "+this.name+" 年龄: "+this.age);
}
}
//静态初始化
public class Test7 {
public static void main(String[] args){
Person[] per = new Person[]{
new Person("张三",16),
new Person("李四",18),
new Person("王五",20)};
for(int i = 0;i < per.length;i++){
per[i].getPrint();
}
}
}
|
|
运行结果:
姓名: 张三 年龄: 16
姓名: 李四 年龄: 18
姓名: 王五 年龄: 20
|
|