java基礎 ---- 一維數組


為什么要使用數組: 因為不使用數組計算多個變量的時候太繁瑣,不利於數據的處理。

--------   數組也是一個變量,是存儲一組相同類型的變量

聲明一個變量就是在內存中划出一塊合適的空間

聲明一個數組就是在內存中划出一塊連續的空間

數組長度就是數組存放了多少個數,最大下標等於數組長度減一

數組中所有的元素必須屬於相同的數據類型

-----------  如何使用數組

 

 

 

 

 

 

 

注意: 在邊聲明邊賦值的時候不要再聲明長度了   [] 中不要在寫值了偶

邊聲明邊賦值的時候數組的長度就被確定了,不能在往數組里面添加數字了

----   練習

import java.util.Scanner;

public class ArrayDemo {

    /**
     * 使用數組計算平均分
     */
    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];    //成績累加
        }
        /*//使用foreach遍歷
        for(int i = 0; i < scores.length; i++){
            scores[i] = input.nextInt();
        }
        for(int score:scores){
            sum+=score;
        }*/
        //計算並輸出平均分
        System.out.println("學員的平均分是:" + (double)sum/scores.length);
    }
}

 

 ----- 使用數組的常見錯誤

1、直接賦值的時候不需要寫長度  但是不賦值的話要寫長度,    int[] scores = new int[];   

2、數組下標越界異常      下標超過了數組長度減一的值

3、在進行創建數組並賦值的時候要放在同一條語句中

----  猜數游戲

import java.util.*;

public class GuessData {

    public static void main(String[] args) {
        int[] list = new int[] { 8, 4, 2, 1, 23, 344, 12 }; // 創建數組並賦值
        
        int sum=0;
        //循環輸出數列的值
        //求數列中所有數值的和
        for(int num:list){
            System.out.println(num);
            sum+=num;
        }
        System.out.println("數組元素之和為:"+sum);

        Scanner input = new Scanner(System.in);
        System.out.print("請輸入一個整數: ");
        int guess = input.nextInt();
        boolean isCorrect = false;
        for (int i = 0; i < list.length; i++) {
            if (guess == list[i]) {
                isCorrect = true;
                break;
            }
        }
        if (isCorrect) {
            System.out.println("猜對了!");
        } else {
            System.out.println("Sorry!");
        }
    }

}

-----   求最大值

import java.util.Scanner;

public class MaxScore {

    /**
     * 求數組最大值
     */
    public static void main(String[] args) {
        int[] scores = new int[5];
        int max = 0;    //記錄最大值
        System.out.println("請輸入5位學員的成績:");
        Scanner input = new Scanner(System.in);
        for(int i = 0; i < scores.length; i++){
            scores[i] = input.nextInt();
        }
        //計算最大值
        //使用max存儲擂主初始值:第一個元素為擂主
        max = scores[0];
        //循環打擂
        for(int i = 1; i < scores.length; i++){
            if(scores[i] > max){
                max = scores[i];
            }
        }
        System.out.println("考試成績最高分為:" + max);
    }
}

----------  插入數值

有一個降序排列的數組,新增一個數字,也要保持降序排列

注要就是把插入的數值與數組中的值依次進行比較,找到第一個數值比他小的位置,就是他要插入的位置,然后在把他下標往后面的移動一位,

import java.util.*;

public class Insert {

    public static void main(String[] args) {
        int[] list = new int[6]; // 長度為為6的數組
        list[0] = 99;
        list[1] = 85;
        list[2] = 82;
        list[3] = 63;
        list[4] = 60;

        int index = list.length; // 保存新增成績插入位置
        System.out.println("請輸入新增成績: ");
        Scanner input = new Scanner(System.in);
        int num = input.nextInt(); // 輸入要插入的數據
        // 找到新元素的插入位置
        for (int i = 0; i < list.length; i++) {
            if (num > list[i]) {
                index = i;
                break;
            }
        }
        // 元素后移
        for (int j = list.length - 1; j > index; j--) {
            list[j] = list[j - 1]; // index下標開始的元素后移一個位置
        }
        list[index] = num;// 插入數據
        System.out.println("插入成績的下標是:" + index);
        System.out.println("插入后的成績信息是: ");
        for (int k = 0; k < list.length; k++) { // 循環輸出目前數組中的數據
            System.out.print(list[k] + "\t");
        }

    }

}

-----  統計數組中奇數和偶數的個數

package cn.jbit.lesson3;

import java.util.Scanner;

/**
 * 統計數組中的奇數和偶數的個數
 *
 */
public class ArrayEx {
    public static void main(String[] args) {
        int[] array = new int[8];
        Scanner input = new Scanner(System.in);
        int count1 = 0;  //記錄奇數的個數
        int count2 = 0;  //記錄偶數的個數
        
        //從控制台接收8個整數,分別統計奇數和偶數的個數
        for(int i=0;i<array.length;i++){
            System.out.print("請輸入第"+(i+1)+"個整數:");
            array[i] = input.nextInt();
            if(array[i]%2==0){
                count1++;
            }else{
                count2++;
            }
        }
        System.out.println();
        System.out.println("奇數的個數是:"+count2+"。");
        System.out.println("偶數的個數是:"+count1+"。");
    }
}

----  數組倒敘復制輸出

 1 package cn.jbit.lesson3;
 2 /**
 3  * 數組倒序復制輸出
 4  * @author boge
 5  *
 6  */
 7 public class ArrayEx2 {
 8     public static void main(String[] args) {
 9         int[] array = new int[10];  //源數組
10         int[] newArray = new int[10];  //目標數組
11         for(int i=0;i<array.length;i++){
12             array[i] = i;  //元素的值等於其下標
13         }
14         System.out.println("原數組:");
15         for(int num:array){
16             System.out.print(num+"\t");
17         }
18         
19         System.out.println();
20         
21         int index = array.length-1;
22         
23         for(int i=0;i<newArray.length;i++){
24             newArray[i] = array[index--];
25         }
26         
27         System.out.println("新數組:");
28         for(int num:newArray){
29             System.out.print(num+"\t");
30         }
31     }
32 }

------ 去除數組中的0

/**
 * 去除數組中的0
 *
 */
public class ArrayEx3 {
    public static void main(String args[]){
        int oldArr [] = {11,31,23,54,0,0,77,90,0,5,42,71,63,79,60,53} ;
        
        int count = 0 ;  //記錄數組中不為0的元素個數
        for(int i = 0 ; i < oldArr.length; i++){
            if(oldArr[i] != 0){
                count ++ ;    // 統計個數
            }
        }
        int newArr [] = new int[count] ;    // 新數組
        
        //將原數組中不為0的元素保存到新數組中
        int j = 0 ;    // 控制新數組的下標
        for(int i = 0 ; i < oldArr.length; i++){
            if(oldArr[i] != 0){
                newArr[j++] = oldArr[i] ;
            }
        }
        //輸出新數組
        for(int i = 0 ; i< newArr.length ; i++){
            System.out.print(newArr[i] + "\t") ;
        }
        
    }
    
}

----  合並數組

 1 public class ArrayEx4 {
 2     public static void main(String args[]){
 3         int arrayA [] = new int[] {1,7,9,11,13,17,19} ;
 4         int arrayB [] = new int[] {2,4,6,8,10} ;
 5         
 6         int len = arrayA.length + arrayB.length ;    // 新數組的大小
 7         int arrayC[] = new int[len] ;                // 新數組
 8         
 9         System.arraycopy(arrayA,0,arrayC,0,arrayA.length) ;    // 拷貝第一個數組
10         System.arraycopy(arrayB,0,arrayC,arrayA.length,arrayB.length) ;    // 拷貝第二個數組
11         java.util.Arrays.sort(arrayC) ;
12         
13         
14         for(int x = 0 ; x< arrayC.length ; x++){
15             System.out.print(arrayC[x] + "、") ;
16         }
17     }
18 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM