java数组的四个要素_Java零基础系列教程04Java数组
配套視頻教程
問題Java考試結束后,老師給張浩分配了一項任務,讓他計算全班(30人)的平均分int stu1 = 95;
int stu2 = 89;
int stu3 = 79;
int stu4 = 64;
int stu5 = 76;
int stu6 = 88;
……
avg = (stu1+stu2+stu3+stu4+stu5…+stu30)/30;
數組
數組是一個變量,存儲相同數據類型的一組數據
聲明一個變量就是在內存空間劃出一塊合適的空間
聲明一個數組就是在內存空間劃出一串連續的空間
數組基本要素標識符:數組的名稱,用于區分不同的數組
數組元素:向數組中存放的數據
元素下標:對數組元素進行編號,從0開始,數組中的每個元素都可以通過下標來訪問
元素類型:數組元素的數據類型
數組長度固定不變,避免數組越界
數組中的所有元素必須屬于相同的數據類型
使用數組步驟:
聲明數組: 告訴計算機數據類型是什么int[ ] score1; //Java考試成績
int score2[ ]; //oracle考試成績
String[ ] name; //學生姓名
分配空間: 告訴計算機分配幾個連續的空間score = new int[30];
avgAge = new int[6];
name = new String[30];
聲明數組并分配空間
數據類型[ ] 數組名 = new 數據類型[大小] ;
賦值:向分配的格子里放數據
score[0] = 89;
score[1] = 79;
score[2] = 76;
……
方法1: 邊聲明邊賦值
int[ ] score = {89, 79, 76};
int[ ] score = new int[ ]{89, 79, 76};方法2:動態地從鍵盤錄入信息并賦值
Scanner input = new Scanner(System.in);
for(int i = 0; i < 30; i ++){score[i] = input.nextInt();
}4. 對數據進行處理:計算5位學生的平均分
int [ ] score = {60, 80, 90, 70, 85};
double avg;
avg = (score[0] + score[1] + score[2] + score[3] + score[4])/5;訪問數組成員:使用“標識符[下標]”
int [ ] score = {60, 80, 90, 70, 85};
int sum = 0;
double avg;
for(int i = 0; i < score.length; i++){sum = sum + score[i];
}
avg = sum / score.length;### 例子
>計算全班學員的平均分
public static void main(String[ ] args) {int[ ] scores = new int[5]; //成績數組
int sum = 0; //成績總和
Scanner input = new Scanner(System.in);
System.out.println("請輸入5位學員的成績:");
for(int i = 0; i < scores.length; i++){
scores[i] = input.nextInt();
sum = sum + scores[i]; //成績累加
}
System.out.println("平均分是:" + (double)sum/scores.length);
}### 數組使用常見錯誤
public class ErrorDemo1 {public static void main(String[ ] args){
int[ ] score = new int[ ];
score[0] = 89;
score[1] = 63;
System.out.println(score[0]);
}
}
public class ErrorDemo2 {public static void main(String[ ] args) {
int[ ] scores = new int[2];
scores[0] = 90;
scores[1] = 85;
scores[2] = 65;
System.out.println(scores[2]);
}
}
public static void main(String[ ] args){int[ ] score = new int[5];
score = {60, 80, 90, 70, 85};
int[ ] score2;
score2 = {60, 80, 90, 70, 85};
}#### 一個練習
> 有一個數列:8,4,2,1,23,344,12
循環輸出數列的值
求數列中所有數值的和
猜數游戲:從鍵盤中任意輸入一個數據,判斷數列中是否包含此數
// 有一個數列:8,4,2,1,23,344,12
// 循環輸出數列的值
// 求數列中所有數值的和// 猜數游戲:從鍵盤中任意輸入一個數據,判斷數列中是否包含此數
int[] array = {8,4,2,1,23,344,12};
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入一個數");
int num = scanner.nextInt();
int i = 0;
//拿數組中的每一個元素和num比較,如果想等,輸出包含,否則,輸出不包含
for(i = 0; i < array.length; i++)
{
if(array[i]==num)
{
System.out.println("包含");
break;
}
}
//說明循環了一圈都沒有發現用戶輸入的值
if(i==array.length)
{
System.out.println("不包含");
}### 數組排序
>循環錄入5位學員成績,進行升序排列后輸出結果
使用java.util.Arrays類
java.util包提供了許多工具類
Arrays類提供操作數組的方法,例排序、查詢
Arrays類的sort()方法: 對數組進行升序排列……
int[] scores = new int[5]; //成績數組
Scanner input = new Scanner(System.in);
System.out.println("請輸入5位學員的成績:");
for(int i = 0; i < scores.length; i++){
scores[i] = input.nextInt();
}
Arrays.sort(scores);
System.out.print("學員成績按升序排列:");
for(int i = 0; i < scores.length; i++){
System.out.print(scores[i] + " ");
}### 查找數組中的最大值
>從鍵盤輸入本次Java考試五位學生的成績,求考試成績最高分
// 從鍵盤輸入本次Java考試五位學生的成績,求考試成績最高分//將5個成績保存到數組中,
//然后,遍歷數組,找出數組中最大的數
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入成績");
int[] scores = new int[5];
//將5個成績保存到數組中,
for(int i = 0; i < scores.length; i++)
{
System.out.println("輸入第" + (i + 1) + "次成績");
scores[i] = scanner.nextInt();
}
//然后,遍歷數組,找出數組中最大的數
int max = scores[0];讓max等于數組中第一個元素
for(int i = 1; i < scores.length; i++)
{
if(max < scores[i])
{
max = scores[i];//誰比他大,他就變成誰
}
}
System.out.println("最大值是" + max);### 數組插入算法
>有一組學員的成績{99,85,82,63, 60},將它們按升序排列。要增加一個學員的成績,將它插入成績序列,并保持升序。

分析:
將成績序列保存在長度為6的數組中
通過比較找到插入位置
將該位置后的元素后移一個位置
將增加的學員成績插入到該位置
// 有一組學員的成績{99,85,82,63, 60},將它們按升序排列。
// 要增加一個學員的成績,將它插入成績序列,并保持升序int[] scores = {99,85,82,63, 60};
Arrays.sort(scores);
for(int i = 0; i < scores.length; i++)
{
System.out.print(scores[i]+" ");
}
// 要增加一個學員的成績,將它插入成績序列,并保持升序
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入下一個學生成績");
int input = scanner.nextInt();
int pos = 0;//用戶輸入的數在新數組中正確的插入位置
//遍歷原始數組,找到要插入的位置
for(int i = 0; i < scores.length; i++)
{
if(input <= scores[i])
{
pos = i;
break;
}
}
//再建一個新的數組,包含6個元素
int[] scores2 = new int[6];
//拷貝舊數組從0開始
// 到pos位置的數到對應新數組同樣下標中
for(int i = 0; i < pos; i++)
{
scores2[i] = scores[i];
}
scores2[pos] = input;
//拷貝舊數組從pos+1開始
// 到舊數組長度位置的數到對應新數組同樣下標中
for(int i = pos+1; i < scores2.length; i++)
{
scores2[i] = scores[i-1];
}
for(int i = 0; i
{
System.out.print(scores2[i]+" ");
}### 字符逆序輸出
將 一組亂序的字符進行排序
進行升序和逆序輸出

1.創建數組存儲原字符序列。
2.利用Array類的sort(?)方法對數組進行排序,并循環輸出。
3. 從最后一個元素開始,將數組中的元素逆序輸出。char[] charArray2 = {'a','c','u','b','e','p','f','z'};
//字符串可以看成是字符數組
String str = "abcefpuz";
System.out.println(charArray2.length);
System.out.println(charArray2);
Arrays.sort(charArray2);
System.out.println(charArray2);
for(int i = charArray2.length - 1; i >= 0; i--)
{
System.out.print(charArray2[i]);
}在上一個練習的基礎上改進:
向上一個練習中得到的升序字符序列中插入一個新的字符,要求插入之后字符序列仍保持有序

### 字符串數組
>定義一個字符串數組,查找某個字符串在數組中出現的次數
String[] array = {"zhangsan","lisi","wangwu","lisi"};String name = "wangwu";
int count = 0;//count計數,數字num在數組中出現的次數
for(int i = 0; i < array.length; i++)
{
if(array[i].equals(name))
{
count++;
}
}
System.out.println(count);### 作業
1.歌手打分:
在歌唱比賽中,共有10位評委進行打分,在計算歌手得分時,去掉一個最高分,去掉一個最低分,然后剩余的8位評委的分數進行平均,就是該選手的最終得分。輸入每個評委的評分,求某選手的得分。
2.現在有如下一個數組:
int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
要求將以上數組中的0項去掉,將不為0的值存入一個新的數組,生成新的數組為
總結
以上是生活随笔為你收集整理的java数组的四个要素_Java零基础系列教程04Java数组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中使用什么导入模块-pyth
- 下一篇: java美元兑换,(Java实现) 美元