java二维数组初始化,遍历与查找
java二维数组初始化,遍历与查找
一维数组初始化
1 静态数组
例如:
float[] stuScores = {80.0f,70.0f,90.0f,66.5f};
int[] nums = {8,77, 90,6};
char[] chs = {'a','b', 'c','d'};
String[] name = new String[]{"张三","李四","王五"};
2 动态数组
变量类型[] 数组名 = new 数据类型 [ 数组长度 ];
例如:
int[] scores = new int[2];
int[] scores;
scores = new int[2];
二维数组初始化
1 静态数组
int[][] scores = {{1,2},{3,4},{5,6}};
2 动态数组
int[][] scores = new int[2][3];
int[][] scores;
scores = new int[2][3];
int[][] scores = new int[2][];
scores[0] = new int[2];
scores[1] = new int[3];
3 遍历输出
int[][] scores = {
{1,2},{3,4},{5,6}
};
for(int i = 0;i<scores.length;i++){
for(int j = 0;j<scores[i].length;j++){
System.out.print(scores[i][j]);
}
System.out.println();
}
实际输出
12
34
56
3 查找
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] name = new String[]{"张三","张三丰","张无忌","王二","张富贵"};
String name1 = sc.next();
for(int i=0;i<name.length;i++){
if(name1.equals(name[i])){
System.out.println(name[i]+"在数组的第"+(i+1)+"个位置");
}
}
}
}

浙公网安备 33010602011771号