数组
数组是使用同一个变量名来保存一组数据。
数组通过其编号来区别数据,这个编号我们叫数组的下标。
数组的下标是从0开始的。
数组的位置仅仅保存首地址
数组的下标在内存中即为偏移量
数组的数据类型表示数组中的所有数据的数据类型
换而言之,数组中的所有数据的数据类型是一致的
数组的长度是固定的,创建数组的时候内存即分配了
语法格式:
表示
数据类型[] 变量名;
数据类型 变量名[];
定义
数据类型[] 变量名称=new 数据类型[数组的长度];
数据类型[] 变量名称={数据1,数据2,……};
数组的操作
存
数组的名称[名称]=10;
取
数据类型 变量名=数组的名称[下标]
package zy;
import java.util.*;
public class sz {
private static Scanner input;
public static void main(String[] args)
{
input = new Scanner(System.in);
String[] kc={"语文","数学","英语"};
int[] cj=new int[3];
int i,zf=0;
float pjf;
for(i=1;i<=3;i++)
{
System.out.print("请输入"+kc[i-1]+"的成绩:");
cj[i-1]=input.nextInt();
zf+=cj[i-1];
}
System.out.print("你的");
for(String k:kc)
{
System.out.print(k+" ");
}
System.out.print("成绩分别为:");
for(int j:cj)
{
System.out.print(j+" ");
}
System.out .print("\n总分为:"+zf);
pjf=zf/kc.length;
System.out .print("\n平均分为:"+pjf);
}
}
二维数组
语法格式:
数据类型[][] 数组名;
数据类型 数组名;
数据类型[][] 数组名=new 数据类型[行数][列数];
数据类型[][]数组名={{},{},{},{}}
存
数据名[行下标][列下标]=值;
取
数据类型 变量名=数据名[行下标][列下标]
三维数组代码实例
可以实现城市查询`
package zy;
import java.util.*;
public class ssq {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[][][] ssq = {
{ { "湖南" },
{ "长沙", "岳麓", "芙蓉", "开福", "雨花", "星沙" },
{ "株洲", "荷塘", "天元", "石峰" },
{ "湘潭", "九华", "雨湖" } ,
{"衡阳,衡山"},
{"岳阳","平江","湘阴","汨罗"},
{"益阳","赫山","桃江"}
},
{ { "云南" },
{ "昆明", "呈贡区","盘龙区","五华区","官渡区","西山区","晋宁区","东川区","安宁市","富民县","宜良县","嵩明县","石林彝族自治县","禄劝彝族苗族自治县","寻甸回族彝族自治县"},
{"曲靖","麒麟区","沾益区","宣威市","马龙县","富源县","罗平县","师宗县","陆良县","会泽县"},
{ "玉溪" ,"红塔区","江川区","澄江县","通海县","华宁县","易门县","峨山彝族自治县","新平彝族傣族自治县","元江哈尼族彝族傣族自治县"},
{"丽江","古城区","永胜县","华坪县","玉龙纳西族自治县","宁蒗彝族自治县"},
{ "普洱","思茅区","宁洱哈尼族彝族自治县","墨江哈尼族自治县","景东彝族自治县","景谷傣族彝族自治县","镇沅彝族哈尼族拉祜族自治县","江城哈尼族彝族自治县","孟连傣族拉祜族佤族自治县","澜沧拉祜族自治县","西盟佤族自治县"},
{"大理","大理市","祥云县","宾川县","弥渡县","永平县","云龙县","洱源县","剑川县","鹤庆县","漾濞彝族自治县","南涧彝族自治县","巍山彝族回族自治县"},
{"红河","蒙自市","个旧市","开远市","弥勒市","建水县","石屏县","泸西县","绿春县","元阳县","红河县","金平苗族瑶族傣族自治县","河口瑶族自治县","屏边苗族自治县"},
{"西双版纳","景洪市","勐海县","勐腊县"},
},
{
{"广东"},
{"广州","白云", "天河", "越秀", "花都"},
{"深圳","罗湖", "福田", "南山", "宝安", "龙岗", "盐田"}
}
};
int i, j, k, m, n, f = 0;
for (i = 0; i < ssq.length; i++) {
for (j = 0; j < ssq[i].length; j++) {
for (k = 0; k < ssq[i][j].length; k++) {
System.out.print(String.valueOf(i) + String.valueOf(j) + String.valueOf(k) + ":" + ssq[i][j][k]+" ");
}
System.out.println();
}
System.out.println();
}
System.out.print("请输入一个城市");
String City = input.nextLine();
for (i = 0; i < ssq.length; i++) {
for (j = 0; j < ssq[i].length; j++) {
for (k = 0; k < ssq[i][j].length; k++) {
if (City.equals(ssq[i][j][k])) {
if ((j == 0) && (k == 0)) {
System.out.print("这是一个省,它下辖 ");
n = j + 1;
for (m = j + 1; m < ssq[i].length; m++)
System.out.print(ssq[i][m][k] + " ");
if (m == n) {
System.out.print("的市为0");
} else {
System.out.print("等区");
}
}
if ((j != 0) && (k == 0)) {
System.out.print("这是一个市,它属于" + ssq[i][0][k] + "省,它下辖 ");
n = k + 1;
for (m = k + 1; m < ssq[i][j].length; m++)
System.out.print(ssq[i][j][m] + " ");
if (m == n) {
System.out.print("的区为0");
} else {
System.out.print("等区");
}
}
if ((j != 0) && (k != 0))
System.out.print("这是一个区,它属于" + ssq[i][0][0] + "省" + ssq[i][j][0] + "市");
f = 1;
}
}
}
}
if (f == 0)
System.out.print("这个城市没有在我的资料中存在。");
}
}