日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数组的运用

發(fā)布時(shí)間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数组的运用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、數(shù)組的概念以及定義:

? ? ? 1、 數(shù)組是存儲(chǔ)同一種數(shù)據(jù)類型多個(gè)元素的集合,也可以看作是一個(gè)容器。

? ? ? 2、數(shù)組既可以存儲(chǔ)基本數(shù)據(jù)類型,也可以存儲(chǔ)引用數(shù)據(jù)類型。

二、一維數(shù)組:

1、一維數(shù)組的定義:

? ? ? ?數(shù)據(jù)類型 [ ] 數(shù)組名 = new 數(shù)據(jù)類型 [ 數(shù)組長(zhǎng)度 ] ;

2、數(shù)組的初始化就是為數(shù)組開辟連續(xù)的內(nèi)存空間,并為每個(gè)數(shù)組元素賦予一個(gè)值。

? ? ? ?a.動(dòng)態(tài)初始化:只給出長(zhǎng)度,由系統(tǒng)給定初始化值。

? ? ? ? ? ? ?數(shù)據(jù)類型 [ ] 數(shù)組名 = new 數(shù)據(jù)類型 [ 數(shù)組長(zhǎng)度 ];

? ? ? ?b.靜態(tài)初始化:

? ? ? ? ? ? ?數(shù)據(jù)類型 [ ] 數(shù)組名 = new 數(shù)據(jù)類型 {元素1,元素2,....,元素n};

3、一維數(shù)組的遍歷:

? ? ? ?? ? ? ? ? ? public static void print(int[ ] arr)

? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ? for (int i = 0;i < arr.length ;i++ ) ? ? ?//arr.length數(shù)組的長(zhǎng)度

? ? ? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?System.out.print(arr[i] + " ");
? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ?}

4、獲取一維數(shù)組中元素的最大值:

? ? ? ? ? ? ? ? ? ?public static void main(String[] args)
? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ? int[] arr = {12,45,3,15,62,17,85,42};
? ? ? ? ? ? ? ? ? ? ? ? ? int max = getMax(arr);
? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println(max);
? ? ? ? ? ? ? ? ? ?}

? ? ? ? ? ? ? ? ? public static int getMax(int[ ] arr)

? ? ? ? ? ? ? ? ? ?{

? ? ? ? ? ? ? ? ? ? ? ? ? int max = arr[ 0 ];

? ? ? ? ? ? ? ? ? ? ? ? ? for(i = 1;i < arr.length;i++)

? ? ? ? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(max < arr[ i ])

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?max = arr[ i ];

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}

? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? return max;

? ? ? ? ? ? ? ? ? ? ?}

5、一維數(shù)組元素對(duì)調(diào):

? ? ? ? ? ? public static void reverseArray(int[] arr) {
????????????????for (int i = 0;i < arr.length / 2 ; i++) {
????????????????????//arr[0]和arr[arr.length-1-0]交換
????????????????????//arr[1]和arr[arr.length-1-1]交換
????????????????????//arr[2]和arr[arr.lentth-1-2]
????????????????????//...
????????????????????int temp = arr[i];
????????????????????arr[i] = arr[arr.length-1-i];
????????????????????arr[arr.length-1-i] = temp;
????????????????}
????????????}

三、二維數(shù)組:

1、二維數(shù)組的定義:

? ? ? ? ? 數(shù)組類型[ ][ ] ?數(shù)組名 = new 數(shù)組類型[m][n];

2、二維數(shù)組的另外兩種定義方式:

? ? ? ? ? a.數(shù)據(jù)類型 數(shù)組名[][] = new 數(shù)據(jù)類型[m][n];

? ? ? ? ? b.數(shù)據(jù)類型[] 數(shù)組名[] = new 數(shù)據(jù)類型[m][n];

3、注意下面定義的區(qū)別:

? ? ? ? ??? int x;
????????????int y;
????????????int x,y;
????????????
????????????int[] x;
????????????int[] y[];
????????????
????????????int[] x,y[];????x是一維數(shù)組,y是二維數(shù)組

4、二維數(shù)組的遍歷:

? ? ? ? ? ? int[][] arr = {{1,2,3},{4,5},{6,7,8,9}};
????????????for (int i = 0;i < arr.length ;i++ ) {????????????//獲取到每個(gè)二維數(shù)組中的一維數(shù)組
????????????????for (int j = 0;j < arr[i].length ;j++ ) {????//獲取每個(gè)一維數(shù)組中的元素
????????????????????System.out.print(arr[i][j] + " ");
????????????????}
????????????????System.out.println();
????????????}

5、二維數(shù)組的求和:

? ? ? ? 需求:公司年銷售額求和
????????某公司按照季度和月份統(tǒng)計(jì)的數(shù)據(jù)如下:單位(萬元)
????????第一季度:22,66,44
????????第二季度:77,33,88
????????第三季度:25,45,65
????????第四季度:11,66,99
????????
????????int[][] arr = {{22,66,44},{77,33,88},{25,45,65},{11,66,99}};

????????int sum = 0;????????????????????????????????????//定義變量,記錄每次相加的結(jié)果
????????for (int i = 0;i < arr.length ;i++ ) {????????????//獲取每一個(gè)一維數(shù)組
????????????for (int j = 0;j < arr[i].length ;j++ ) {????//獲取每一個(gè)一維數(shù)組中的元素
????????????????sum = sum + arr[i][j];????????????????????//累加
????????????}
????????}

????????System.out.println(sum);

?

轉(zhuǎn)載于:https://www.cnblogs.com/shw110/p/7198071.html

總結(jié)

以上是生活随笔為你收集整理的数组的运用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。