java數(shù)組

數(shù)組

數(shù)組是一個(gè)變量,存儲(chǔ)相同數(shù)據(jù)類型的一組數(shù)據(jù)

數(shù)組基本要素

標(biāo)識(shí)符:數(shù)組的名稱,用于區(qū)分不同的數(shù)組
數(shù)組元素:向數(shù)組中存放的數(shù)據(jù)
元素下標(biāo):對數(shù)組元素進(jìn)行編號(hào),從0開始,數(shù)組中的每個(gè)元素都可以通過下標(biāo)來訪問
元素類型:數(shù)組元素的數(shù)據(jù)類型


數(shù)組長度固定不變,避免數(shù)組越界
數(shù)組中的所有元素必須屬于相同的數(shù)據(jù)類型
 int[] count=new int[8]; // 創(chuàng)緊數(shù)組
              count[1]=90;       //數(shù)組名[下標(biāo)]=xxx
        int m=count[1]          //賦值給m

數(shù)組語法與運(yùn)用

       int[] array=new int[10];
        for (int i=0;i<10;i++){
            array[i]=i+1;
        }

        int total=0;
        for (int i=0;i<10;i++){
            total=total+array[i];
        }
        System.out.println(total);

數(shù)組產(chǎn)生的隨機(jī)數(shù)

      int[] array = new int[5];
        for(int i = 0; i < 5; i++)
        {
            array[i] = (int)(Math.random()*10);
        }

        for(int i = 0; i < 5; i++)
        {
            System.out.println(array[i]);
        }
有一個(gè)數(shù)列:8,4,2,1,23,344,12
循環(huán)輸出數(shù)列的值
求數(shù)列中所有數(shù)值的和
猜數(shù)游戲:從鍵盤中任意輸入一個(gè)數(shù)據(jù),判斷數(shù)列中是否包含此數(shù)
        int[] array = {8,4,2,1,23,344,12};

        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入一個(gè)數(shù)");
        int num = scanner.nextInt();
        int i = 0;
        //拿數(shù)組中的每一個(gè)元素和num比較,如果想等,輸出包含,否則,輸出不包含
        for(i = 0; i < array.length; i++)
        {
            if(array[i]==num)
            {
                System.out.println("包含");
                break;
            }
        }

        //說明循環(huán)了一圈都沒有發(fā)現(xiàn)用戶輸入的值
        if(i==array.length)
        {
            System.out.println("不包含");
        }

查找數(shù)組中的最大值

//        從鍵盤輸入本次Java考試五位學(xué)生的成績,求考試成績最高分
        //將5個(gè)成績保存到數(shù)組中,
        //然后,遍歷數(shù)組,找出數(shù)組中最大的數(shù)
        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入成績");
        int[] scores = new int[5];
        //將5個(gè)成績保存到數(shù)組中,
        for(int i = 0; i < scores.length; i++)
        {
            System.out.println("輸入第" + (i + 1) + "次成績");
            scores[i] = scanner.nextInt();
        }
        //然后,遍歷數(shù)組,找出數(shù)組中最大的數(shù)
        int max = 0;//假設(shè)max最大
        for(int i = 0; i < scores.length; i++)
        {
            if(max < scores[i])
            {
                max = scores[i];//誰比他大,他就變成誰
            }
        }

        System.out.println("最大值是" + max);

Arrays類的sort()方法: 對數(shù)組進(jìn)行升序排列

循環(huán)錄入5位學(xué)員成績,進(jìn)行升序排列后輸出結(jié)果

int[] scores = new int[5]; //成績數(shù)組
    Scanner input = new Scanner(System.in);
    System.out.println("請輸入5位學(xué)員的成績:");
    for(int i = 0; i < scores.length; i++){
        scores[i] = input.nextInt();
    }

    Arrays.sort(scores);            
    System.out.print("學(xué)員成績按升序排列:");
    for(int i = 0; i < scores.length; i++){
        System.out.print(scores[i] + " ");
    }

Char的數(shù)組運(yùn)用

Char 逆序輸出
將 一組亂序的字符進(jìn)行排序
進(jìn)行升序和逆序輸出


char[] charArray2 = {'a','c','u','b','e','p','f','z'};
//字符串可以看成是字符數(shù)組
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]);
}

定義一個(gè)字符串?dāng)?shù)組,查找某個(gè)字符串在數(shù)組中出現(xiàn)的次數(shù)

String[] array = {"zhangsan","lisi","wangwu","lisi"};

        String name = "wangwu";
        int count = 0;//count計(jì)數(shù),數(shù)字num在數(shù)組中出現(xiàn)的次數(shù)
        for(int i = 0; i < array.length; i++)
        {
            if(array[i].equals(name))
            {
                count++;
            }
        }

        System.out.println(count);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容