Java数组习题

/*

* 1、编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,

* 在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)*/

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、将一个字符数组的值(education)拷贝到另一个字符数组中。*/

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个整数(1,6,2,3,9,4,5,7,8)的数组,

* 先排序,然后输出排序后的数组的值

* 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个多维数组分别是 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 依次类推。(知识点:多维数组定义和创建、数组遍历、数组元素访问)

*/

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型二维数组(长度分别为5、4,

* 值自己设定)的值。(知识点:数组定义和创建、多维数组初始化、数组遍历)

*/

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个整数(18,25,7,36,13,2,89,63)的数组中

* 找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问)

*/

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.将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问)*/

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

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);

}

}

运行图:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 【程序1】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔...
    开心的锣鼓阅读 3,344评论 0 9
  • 第四天 数组【悟空教程】 第04天 Java基础 第1章数组 1.1数组概念 软件的基本功能是处理数据,而在处理数...
    Java帮帮阅读 1,612评论 0 9
  • Java经典问题算法大全 /*【程序1】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子...
    赵宇_阿特奇阅读 1,895评论 0 2
  • DAY 05 1、 public classArrayDemo { public static void mai...
    周书达阅读 738评论 0 0
  • 回溯算法 回溯法:也称为试探法,它并不考虑问题规模的大小,而是从问题的最明显的最小规模开始逐步求解出可能的答案,并...
    fredal阅读 13,726评论 0 89