当前位置:网站首页 > Java基础 > 正文

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] + " ");

}

}

}

运行图:

ea758b739b84

/*

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

}

}

运行图:

ea758b739b84

/*

* 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 + " ");

}

}

}

运行图:

ea758b739b84

/*

* 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 + " ");

java基础编程题数组

}

System.out.println("");

}

}

}

运行图:

ea758b739b84

/*

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

}

}

}

运行图:

ea758b739b84

/*

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

}

}

运行图:

ea758b739b84

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

}

}

运行图:

ea758b739b84

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

}

}

运行图:

版权声明


相关文章:

  • 如何零基础自学java2024-10-20 23:58:06
  • 0基础java在公司做什么2024-10-20 23:58:06
  • java零基础创意广告2024-10-20 23:58:06
  • java基础导出csv2024-10-20 23:58:06
  • java面向对象编程的基础原理2024-10-20 23:58:06
  • java零基础学习在哪可以学2024-10-20 23:58:06
  • java中继承与多态的基础题2024-10-20 23:58:06
  • java方法基础题2024-10-20 23:58:06
  • 零基础想学java需要多久2024-10-20 23:58:06
  • java基础案例4-82024-10-20 23:58:06