javaSE练习3——数组

一、编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。

package com.test;

public class t01 {

public static void main(String[] args) {
// 静态初始化
int i[] = new int[] { 10, 20, 30, 40, 50 };

// 遍历数组
for (int j = 0; j < 5; j++) {
System.out.print(i[j]+" ");
}
}

}

效果图如下:

 

二、将一个字符数组的值(neusofteducation)考贝到另一个字符数组中。

package com.test;

public class t02 {

public static void main(String[] args) {
// 定义源字符数组
char[] copy = { 'n', 'e', 'u', 's', 'o', 'f', 't', 'e', 'd', 'u', 'c', 'a', 't', 'i', 'o', 'n' };

// 定义新字符数组
char[] copyTo = new char[7];

/*
* System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
* src:源数组; srcPos:源数组要复制的起始位置; dest:目的数组; destPos:目的数组放置的起始位置;
* length:复制的长度
*/
System.arraycopy(copy, 2, copyTo, 0, 7);

System.out.println(new String(copyTo));
}

}

效果图如下:

 

三、 给定一个有9个整数(1,6,2,3,9,4,5,7,8})的数组,先排序,然后输出排序后的数组的值。

package com.test;

public class t03 {

public static void main(String[] args) {
// 定义数组
int[] i = { 1, 6, 2, 3, 9, 4, 5, 7, 8 };

java.util.Arrays.sort(i); // 排序

for (int j = 0; j < i.length; j++) {
System.out.print(i[j] + " ");
}
}

}

效果图如下:

 

四、输出一个double型二维数组(长度分别为5、4,值自己设定)的值。

package com.test;

public class t04 {

public static void main(String[] args) {
double[][] dou = new double[5][4];

for (int i = 0; i < dou.length; i++) {
for (int j = 0; j < dou[0].length; j++) {
System.out.println(dou[i][j]);
}
System.out.println();
}

}

}

效果图如下:

 

五、在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。

package com.test;

public class t05 {

public static void main(String[] args) {
int[] arr = { 18, 25, 7, 36, 13, 2, 89, 63 };
int max = arr[0];
int maxIndex = 0;

for (int i = 1; i < arr.length; i++) {
if (max <= arr[i]) {
max = arr[i];
maxIndex = i;
}
}
System.out.println("最大值为:" + max + "\n最大值下标为:" + maxIndex);

}

}

效果图如下:

 

六、

有2个多维数组分别是 2 3 4   和  1 5 2 8

4 6 8       5 9 10 -3

2 7 -5 -18

按照如下方式进行运算。生成一个2行4列的数组。此数组的第1行1列是2*1+3*5+4*2

第1行2列是2*5+3*9+4*7  第2行1列是4*1+6*5+8*2 依次类推。

package com.test;

public class t06 {
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 k = 0; k < a.length; k++) {
for (int i = 0; i < b[0].length; i++) {
int num = 0;
for (int j = 0; j < b.length; j++) {
num += a[k][j] * b[j][i];
}
System.out.println(num + " ");
}
System.out.println("");
}
}

}

效果图如下:

 

七、将一个数组中的元素逆序存放。

package com.test;

import java.util.Scanner;

public class t07 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int arr[] = new int[20];

System.out.println("请输入多个正整数(输入-1表示结束):");
int i = 0, j;
do {
arr[i] = sc.nextInt();
} while (arr[i - 1] != -1);
System.out.println("请输入的数组为:");
for (j = 0; j < i - 1; j++) {
System.out.println(arr[j] + " ");
}
System.out.println("\n数组逆序输出为:");
for (j = i - 2; j >= 0; j = j - 1) {
System.out.println(arr[j] + " ");
}
}

}

 

 

八、将一个数组中的重复元素保留一个其他的清零。

package com.test;

public class t08 {
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;
}
}
}
}

}

 

 

九、给定一维数组{ -10,2,3,246,-100,0,5} ,计算出数组中的平均值、最大值、最小值。

package com.test;

public class t09 {
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);
}

}

效果图如下:

 

猜你喜欢

转载自www.cnblogs.com/ITXiaoBai-liujb/p/10235424.html