Java入门 之 数组

1. 什么是数组

package com.imooc;

public class Demo04 {

	public static void main(String[] args) {
		//定义数组,保存五个数据
		int[] scores = { 78, 93, 97, 84, 63 };
		//输出数组中的第二个元素
		System.out.println("the 2nd element in the array: " + scores[1]);
	}
}

2. 如何使用Java中的数组

  2.1 声明数组

    数据类型[ ]  数组名;  或者  数据类型  数组名[ ];

int[] scores;  //存储成绩的数组,类型为整型
double height[];  //存储高度的数组,类型为浮点型
String[] names;  //存储姓名的数组,类型为字符串

  2.2 分配空间:指定数组可以存储多少个元素

    数组名 = new 数据类型[数组长度]

scores = new int[5];  //长度为5的整数数组
height = new double[5];  //长度为5的浮点型数组
names = new String[5];  //长度为5的字符串数组
int[] scores = new int[5];  //在声明数组的同时为它分配空间

  2.3 赋值:数组中的元素都是通过下标来访问的

scores[0] = 89;
scores[1] = 79;

  2.4 处理数组中的数据

System.out.println("the 1st element in array scores: " + scores[0]);  //输出数组中元素的值
int[] scores = {78, 91, 22, 46};  //声明数组,分配空间,赋值 合并完成
int[] scores = new int[] {78, 91, 34, 56};  //第二个[]内必须为空,不能指定长度

  2.5 示例

package com.imooc;

public class Demo05 {

	public static void main(String[] args) {
		String[] subjects = new String[5];
		subjects[0] = "Oracle";
		subjects[1] = "PHP";
		subjects[2] = "Linux";
		subjects[3] = "Java";
		subjects[4] = "HTML";
		System.out.println("the 4th item of subjects: " + subjects[3]);
	}
}

3. 使用循环操作Java中的数组

int[] scores = {87, 34, 54, 23};  //定义一个长度为4的数组并赋值
for {int i=0; i<scores.length; i++}  //循环变量i从0开始递增直到数组的最大长度scores.length
{
    System.out.println("the" +(i+1)+ "th item in array is: " +scores[i]);
}
package com.imooc;

public class Demo06 {
	public static void main(String[] args) {
		String[] hobby = {"sports", "game", "movie"};
		System.out.println("iterate the item in array hoddy: ");
		for(int i=0; i<hobby.length; System.out.println(hobby[i]), i++);
	}
}
package com.imooc;

public class Demo07 {

	public static void main(String[] args) {
		int[] nums = new int[] {61, 23, 4, 74, 13, 148, 20};
		int max = nums[0];
		int min = nums[0];
		double sum = 0;
		double avg = 0;
		
		for(int i=0; i<nums.length; i++) {
			if(nums[i] > max) {
				max = nums[i];
			};
			if(nums[i] < min) {
				min = nums[i];
			};
			sum += nums[i];
		}
		avg = sum/nums.length;
		
		System.out.println("the max number in the array: " + max);
		System.out.println("the min number in the array: " + min);
		System.out.println("the avg number in the array: " + avg);
	}
}

4. 使用Arrays类操作Java中的数组

  4.1 排序:  Array.sort(数组名);

package com.imooc;
import java.util.Arrays;

public class Demo08 {

	public static void main(String[] args) {
		int[] scores = { 78, 82, 43, 12, 34 };
		Arrays.sort(scores);
		System.out.println("items in array after sorting: ");
		for(int i=0; i<scores.length; i++) {
			System.out.println(scores[i]);
		}
	}
}

  4.2 将数组转换为字符串:  Arrays.toString(数组名);

    - toString()方法将一个数组转换成字符串,该方法按顺序把多个数组元素连接在一起,多个元素之间使用都好和空格隔开。

package com.imooc;

import java.util.Arrays;

public class Demo09 {

	public static void main(String[] args) {
		int[] nums = new int[] {23,5,334,65,78};
		System.out.println(nums);
		System.out.println(nums.length);
		System.out.println("print the elements in array: " + Arrays.toString(nums));
		System.out.println(Arrays.toString(nums).length());
	}
}
package com.imooc;

import java.util.Arrays;

public class Demo10 {

	public static void main(String[] args) {
		String[] hobbies = {"sports", "game", "movie"};
		Arrays.sort(hobbies);
		System.out.println(Arrays.toString(hobbies));
	}
}

5. 使用foreach操作数组:  for(元素类型 元素变量:遍历对象) { 执行的代码}

package com.imooc;

public class Demo11 {

	public static void main(String[] args) {
		String[] hobbies = {"wayne", "zhong", "sheng"};
		System.out.println("***print elements in array by using FOR***");
		for(int i=0; i<hobbies.length; i++) {
			System.out.println(hobbies[i]);
		}
		
		System.out.println();
		System.out.println("***print elements in array by using FOREACH***");
		for(String hobby : hobbies) {
			System.out.println(hobby);
		}
	}
}

6. Java中的二维数组

  6.1 声明数组并分配空间:  

    数据类型[ ][ ]  数组名 = new  数据类型[行的个数][列的个数];  

    或者

    数据类型[ ][ ]  数组名;

    数组名 = new  数据类型[行的个数][列的个数];

    int[][] num = new int[2][3];     //定义一个两行三列的二维数组

  6.2 赋值:  

    数组名[行的索引][列的索引] = 值;

    也可以在声明数组的同时为其赋值

    数据类型[ ][ ]  数组名 = {{值11,值12, 值13}, {值21, 值22, 值23}, {值31, 值32, 值33}};  

    num[0][0] = 12;  //给第1行第1列的元素赋值

  6.3 处理数据:用二重循环输出二维数组中的每一个元素

int[][] num = {{1,2,3}, {4,5,6}};  //定义一个两行三列的二维数组并赋值
for(int i=0; i<num.length; i++){
    for(int j=0; j<num[i].length; j++){
        System.out.println(num[i][j]);
    }
    System.out.println();
}

    - 如果每行的列数不同,则创建的是不规则的二维数组

int[][] num = new int[3][];  //定义一个三行的二维数组
num[0] = new int[2];  //为第一行分配两列
num[1] = new int[3];  //为第二行分配三列
num[2] = new int[4];  //为第三行分配四列
num[0][0] = 1;  //第一行第一列赋值为1
num[1][1] = 2;  //第二行第二列赋值为2
num[2][3] = 3;  //第三行第四列赋值为3
System.out.println(num[0][0]);  //会输出1
System.out.println(num[1][1]);  //会输出2
System.out.println(num[2][3]);  //会输出3
package com.imooc;

public class Demo12 {

	public static void main(String[] args) {
		String[][] names = {{"tom", "jack", "mike"}, {"zs", "ls", "ww"}};
		for(int i=0; i<names.length; i++) {
			for(int j=0; j<names[i].length; j++) {
				System.out.println(names[i][j]);
			}
			System.out.println();
		}
	}
}

  

    

原文地址:https://www.cnblogs.com/wnzhong/p/7716836.html