Java數(shù)組習題

/*

* 1、編寫一個簡單程序,要求數(shù)組長度為5,分別賦值10,20,30,40,50,

* 在控制臺輸出該數(shù)組的值。(知識點:數(shù)組定義和創(chuàng)建、一維數(shù)組初始化)*/

public class Arrayses {

public static void main(String[] args) {

int[] arr = new int[] { 10, 20, 30, 40, 50 };

for (int i = 0; i < arr.length; i++) {

System.out.println(arr[i] + " ");

}

}

}

運行圖:

/*

* 2、將一個字符數(shù)組的值(education)拷貝到另一個字符數(shù)組中。*/

public class ArraysCopy {

public static void main(String[] args) {

char[] c = new char[] { 'e', 'd', 'u', 'c', 'a', 't', 'i', 'o', 'n' };

char[] a = new char[9];

for (int i = 0; i < c.length; i++) {

a[i] = c[i];

}

System.out.println(a);

}

}

運行圖:

/*

* 3、給定一個有9個整數(shù)(1,6,2,3,9,4,5,7,8)的數(shù)組,

* 先排序,然后輸出排序后的數(shù)組的值

* Arrays.sort排序、冒泡排序

*/

public class Bulble {

public static void main(String[] args) {

int[] a = { 1, 2, 8, 6, 4, 9, 3, 4, 6, 4 };

// Arrays.sort()

/*

* java.util.Arrays.sort(a); for(int element : a) {//增強

* System.out.print(element+" "s); }

*/

for (int i = 0; i < a.length - 1; i++) {

for (int j = 0; j < a.length - i - 1; j++) {

if (a[j] > a[j + 1]) {

int temp = a[j];

a[j] = a[j + 1];

a[j + 1] = temp;

}

}

}

for (int element : a) {// 增強

System.out.print(element + " ");

}

}

}

運行圖:

/*

* 4、有2個多維數(shù)組分別是 2 3 4? 和? 1 5 2 8

? 4 6 8? ? 5 9 10 -3

? ? ? ? ? ? 2 7 -5 -18

? 按照如下方式進行運算。生成一個2行4列的數(shù)組。

? 此數(shù)組的第1行1列是2*1+3*5+4*2第1行2列是2*5+3*9+4*7?

? 第2行1列是4*1+6*5+8*2 依次類推。(知識點:多維數(shù)組定義和創(chuàng)建、數(shù)組遍歷、數(shù)組元素訪問)

*/

public class Arrayser {

public static void main(String[] args) {

int a[][] = { { 2, 3, 4 }, { 4, 6, 8 } };

int b[][] = { { 1, 5, 2, 8 }, { 5, 9, 10, -3 }, { 2, 7, -5, -18 } };

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < b[0].length; j++) {

int num = 0;

for (int k = 0; k < b.length; k++) {

num += a[i][k] * b[k][j];

}

System.out.print(num + " ");

}

System.out.println("");

}

}

}

運行圖:

/*

* 5、 輸出一個double型二維數(shù)組(長度分別為5、4,

* 值自己設定)的值。(知識點:數(shù)組定義和創(chuàng)建、多維數(shù)組初始化、數(shù)組遍歷)

*/

public class DoubleArray {

public static void main(String[] args) {

double[][] a = { { 1, 2, 3, 4 }, { 5, 4, 3, 2 }, { 6, 5, 8, 7 }, { 6, 9, 4, 2 }, { 5, 8, 6, 1 } };

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < a[0].length; j++) {

System.out.print(a[i][j] + " ");

}

System.out.println();

}

}

}

運行圖:

/*

* 6、 在一個有8個整數(shù)(18,25,7,36,13,2,89,63)的數(shù)組中

* 找出其中最大的數(shù)及其下標。(知識點:數(shù)組遍歷、數(shù)組元素訪問)

*/

public class FindArray {

public static void main(String[] args) {

int[] a = { 18, 25, 7, 36, 13, 2, 89, 63 };

int max = a[0];

int maxidx = 0;

for (int i = 1; i < a.length; i++) {

if (max <= a[i]) {

max = a[i];

maxidx = i;

}

}

System.out.println("最大值:" + max + "最大值下標:" + maxidx);

}

}

運行圖:

/*8.將一個數(shù)組中的重復元素保留一個其他的清零。(知識點:數(shù)組遍歷、數(shù)組元素訪問)*/

import java.util.Arrays;
public class ClearArray {

public static void main(String[] args) {

int[] arr = { 1, 2, 3, 4, 5, 6, 4, 7, 2, 10 };

for (int i = 0; i < arr.length - 1; i++) {

for (int j = i + 1; j < arr.length; j++) {

if (arr[i] == arr[j]) {

arr[j] = 0;

}

}

}

System.out.println(Arrays.toString(arr));

}

}

運行圖:

/*9、給定一維數(shù)組{ -10,2,3,246,-100,0,5} ,計算出數(shù)組中的平均值、最大值、最小值。(知識點:數(shù)組遍歷、數(shù)組元素訪問)*/

public class CalculateArray {

public static void main(String[] args) {

int arr[] = new int[] { -10, 23, 246, -100, 0, 5 };

int max = arr[0];

int min = arr[0];

int add = arr[0];

for (int i = 0; i < arr.length; i++) {

if (arr[i] < min) {

min = arr[i];

} else if (arr[i] > max) {

max = arr[i];

}

add = add + arr[i];

}

System.out.println("最小值:" + min);

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

System.out.println("平均值:" + add / arr.length);

}

}

運行圖:

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

相關閱讀更多精彩內(nèi)容

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