您好,欢迎来到筏尚旅游网。
搜索
您的当前位置:首页java编程基础—数组练习

java编程基础—数组练习

来源:筏尚旅游网


1、编写一个简单程序,要求数组长度为5,分别赋值10, 20, 30, 40, 50,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)

public class work01 {

public static void main(String[] args) {

// TODO Auto-generated method stub

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

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

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

}

}

}

运行结果:

1

2

2、将一个字符数组的值( neusofteducation)拷贝到另一个字符数组中。(知识点:数组复制)

public class work02 {

public static void main(String[] args) {

// TODO Auto-generated method stub

char[] copyFrom = new char[]{'n','e','u','s','o','f','t','e',

'd','u','c','a','t','i','o','n'};

char[ ] copyTo = new char[16];

System.arraycopy(copyFrom, 0, copyTo, 0, 16);

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

}

}

运行结果:

3

4

3、给定一个有9个整数( 1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。(知识点: Arrays.sort排序、冒泡排序)

public class work03 {

public static void main(String[] args) {

// TODO Auto-generated method stub

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

java.util.Arrays.sort( point );

for(int i=0;i{

System.out.print(point[i]);

}

}

}

5

运行结果:

6

public class Demo03 {

public static void main(String[] args) {

// TODO Auto-generated method stub

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

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

//2次循环 输出2行格式,行数 2行

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

//每行输出4个元素,列数 4列

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

7

int num = 0;

for (int j = 0; j < b.length; j++) {//累加3次,生成具体的新数据

num += a[k][j] * b[j][i];//第一次a[0][0]*b[0][0]

//第二次a[0][1]*b[1][0]

//第三次a[0][2]*b[2][0]

}

System.out.print(num + \" \");//第一个数出来了

}

System.out.println(\"\");

}

}

}

运行结果:

8

9

5、 输出一个double型二维数组(长度分别为5、 4,值自己设定)的值。(知识点:数组定义和创建、数组初始化、数组遍历)

public class work04 {

public static void main(String[] args) {

// TODO Auto-generated method stub

double[][] buffer = new double[][]{{0,1,2,3},{4,5,6,7},

{8,9,10,11},{11,12,13,14},{15,16,17,18}};

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

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

System.out.print(buffer[i][j]);

}

System.out.println();

}

10

}

}

运行结果:

11

6、 在一个有8个整数( 18, 25, 7, 36, 13, 2,, 63)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问)

public class work05 {

public static void main(String[] args) {

// TODO Auto-generated method stub

int[] arr = new int[]{18,25,7,36,13,2,,63};

int max = arr[0];

int maxid = 0;

for(int i = 1;iif(arr[i]>max){

max = arr[i];

maxid = i;

}

12

}

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

System.out.println(\"最大数的下标是\"+maxid);

}

}

运行结果:

13

7、将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)

14

8. 将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问)

public class work06 {

public static void main(String[] args) {

// TODO Auto-generated method stub

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

for(int i = 0;ifor(int j = i+1;jif(arr[i]==arr[j]){

arr[j] = 0;

}

}

System.out.print(arr[i]);

15

}

}

}

运行结果:

16

9、给定一维数组{ -10, 2, 3, 246, -100, 0, 5},计算出数组中的平均值、最大值、最小值。(知识点:数组遍历、数组元素访问)

方法一:使用排序sort();方法

import java.util.*;

public class demo02 {

public static void main(String[] args) {

// TODO Auto-generated method stub

int[] arr = {-10,2,3,246,-100,0,5};

//计算平均值

double sum = 0;//定义数组元素之和

double avg = 0;//定义数组元素之和的平均值

for(int i = 0;isum = sum + arr[i];

17

}

avg = sum/arr.length;

System.out.println(\"数组元素之和:\"+sum);

System.out.println(\"数组元素的平均值:\"+avg);

System.out.println(\"经过排序:\");

java.util.Arrays.sort(arr);

for(int i = 0;iSystem.out.print(arr[i]+\" \");

}

System.out.println();

System.out.println(\"最小值:\"+arr[0]);

System.out.println(\"最大值:\"+arr[arr.length-1]);

}

18

}

运行结果:

方法二:使用一次for循环

public class Demo04 {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

int[] arr = {-10,2,3,246,-100,0,5};

19

int max = arr[0];

int min = arr[0];

int add = arr[0];

for(int i = 1;iadd = add + arr[i];//求和

if(arr[i]>max){

max = arr[i];

}else if(arr[i]min = arr[i];

}

}

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

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

20

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

}

}

运行结果:

21

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- efsc.cn 版权所有 赣ICP备2024042792号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务