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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

java 数组习题

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

2.寫一個函數(shù),返回一個整數(shù)數(shù)組的平均值

package com.fxm.day05.test; public class Day05test{public static void main(String[] args){int[] a1 = {2,9,5,7,4};int n = average(a1);System.out.println(n);}public static int average(int[] a){int sum = 0;for(int i = 0; i < a.length; i++){sum += a[i];}int age = sum/a.length;return age;} }

3.寫一個函數(shù)接受一個整數(shù)數(shù)組a,以及一個整數(shù)n,如果n在數(shù)組中存在,則返回n出現(xiàn)的首 次的下標(biāo);如果不存在,則返回-1。

package com.fxm.day05.test; public class Day05Test3{public static void main(String[] args){int[] a = {2,4,5,6,8};int n = 7;int n1 = test3(a,n);System.out.println(n1);}public static int test3(int[] a,int b){for(int i = 0; i < a.length; i++){if(a[i] == b){System.out.println("找到了");return i;}}return -1;} }

4.寫一個函數(shù)接受一個數(shù)組,打印輸出數(shù)組中的大值和小值。

package com.fxm.day05.test; public class Day05Test4{public static void main(String[] args){int[] a = {3,5,1,7,5,8};test4(a);}public static void test4(int[] a){int max = a[0];int min = a[0];for(int i = 0; i < a.length; i++){if(max < a[i]){max = a[i];}if(min > a[i]){min = a[i];}}System.out.println(max);System.out.println(min);} }

5.寫一個函數(shù)接受一個數(shù)組,把這個數(shù)組中所有元素順序進(jìn)行顛倒。

package com.fxm.day05.test; public class Day05Test5{public static void main(String[] args){int[] a = {2,4,6,8,9};test5(a);}public static void test5(int[] a){for(int i = a.length - 1; i >= 0; i--){System.out.println(a[i]);}} }

7.將上面所有習(xí)題使用可變長參數(shù)替換數(shù)組實現(xiàn)

package com.fxm.day05.test; public class Day05Test6{public static void main(String[] args){int[] a = {2,9,5,7,4};int n1 = average(a);System.out.println(n1);int n = 7;int n2 = test3(n,a);System.out.println(n2);test4(a);test5(a);}public static int average(int... a){int sum = 0;for(int i = 0; i < a.length; i++){sum += a[i];}int age = sum/a.length;return age;}public static int test3(int b,int... a){for(int i = 0; i < a.length; i++){if(a[i] == b){System.out.println("找到了");return i;}}return -1;}public static void test4(int... a){int max = a[0];int min = a[0];for(int i = 0; i < a.length; i++){if(max < a[i]){max = a[i];}if(min > a[i]){min = a[i];}}System.out.println(max);System.out.println(min);}public static void test5(int... a){for(int i = a.length - 1; i >= 0; i--){System.out.println(a[i]);}} }

9.完成數(shù)組的冒泡排序算法
給定一個數(shù)組:int[] a = {1,3,2,7,5} 利用冒泡排序?qū)ζ浒凑諒男〉酱蟮捻樞蚺判?#xff0c;然后輸出結(jié)果。

package com.fxm.day05.test; public class Day05Test9{public static void main(String[] args){int[] a = {1,3,2,7,5};for(int i = 1; i < a.length; i++){for(int j = 0; j < a.length-i; j++){if(a[j] > a[j+1]){int temp = a[j];a[j] = a[j+1];a[j+1] = temp;}}}for(int n = 0; n < a.length; n++){System.out.print(a[n]);}} }

10.使用第二種算法對數(shù)組進(jìn)行排序

package com.fxm.day05.test; public class Day05Test10{public static void main(String[] args){int[] a = {1,3,2,7,5};for(int i = 0; i < a.length; i++){int m = i;for(int j = i + 1; j < a.length; j++){if(a[j] < a[m]){m = j;}}if(i != m){int temp = a[i];a[i] = a[m];a[m] = temp;}}for(int n = 0; n < a.length; n++){System.out.print(a[n]);}} }

11.已知一個二維數(shù)組A 表示一個矩陣,求 其中, 表示矩陣的轉(zhuǎn)置。矩陣轉(zhuǎn)置的含義:表示把一個矩陣行列互換。

package com.fxm.day05.test; public class Day05Test11{public static void main(String[] args){int[][] a = {{1,2,3},{4,5,6}};int[][] b = new int[3][2];for(int i = 0; i < a.length; i++){for(int j = 0; j < a[i].length; j++){b[j][i] = a[i][j];}}for(int i = 0; i < b.length; i++){for(int j = 0; j < b[i].length; j++){System.out.print(b[i][j]+" ");}System.out.println();}} }

總結(jié)

以上是生活随笔為你收集整理的java 数组习题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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