package com.homework8;
/*1、编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,
在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)[必做题]?*/
public class Exe8_1_1 {
public static void main(String[] args) {
int a[]={10,20,30,40,50}; //静态赋值
for (int i : a) {
System.out.print(i +"\t");
}
System.out.println();//动态赋值
for (int i = 0; i < a.length; i++) {
a[i] =(i+1)*10;
}
for (int i : a) {
System.out.print(i +"\t");
}
}
}
![]()
package com.homework8;
/*2、将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。
(知识点:数组复制) [必做题]?*/
public class Exe8_1_2 {
public static void main(String[] args) {
char a[] ={'a','b','c','d','e','f','g'};
char b[] ={'A','B','C','D','E','F','G'};
System.arraycopy(a, 1, b, 1, 4);
for (char c : b) {
System.out.print(c +"\t");
}
}
}
![]()
package com.homework8;
import java.util.Arrays;
/*3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,
然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序)*/
public class Exe8_1_3 {
public static void main(String[] args) {
int a[] =new int[]{1,6,2,3,9,4,5,7,8};
Arrays.sort(a);//Arrays.sort排序
for (int i : a) {
System.out.print(i +"\t");
}
System.out.println();//冒泡排序
for (int i = 0; i < a.length; i++) {
for (int j = i; j < a.length; j++) {
if(a[i]>a[j]){
int x =a[i];
a[i] =a[j];
a[j] =x;
}
}
}
for (int i : a) {
System.out.print(i +"\t");
}
}
}
![]()
package com.homework8;
/*4、 输出一个double型二维数组(长度分别为5、4,值自己设定)的值。
(知识点:数组定义和创建、多维数组初始化、数组遍历)*/
public class Exe8_1_4 {
public static void main(String[] args) {
double a[][] =new double[5][4];
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
a[i][j] =i+j+2;
}
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(a[i][j] +"\t");
}
System.out.println();
}
}
}
![]()
package com.homework8;
/*5、 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中
最大的数及其下标。(知识点:数组遍历、数组元素访问) [必做题]?*/
public class Exe8_1_5 {
public static void main(String[] args) {
int a[] =new int[]{18,25,7,36,13,2,89,63};
int max =0; int maxidx =0;
for (int i = 0; i < a.length; i++) {
if(a[i]>max)
max =a[i]; maxidx =i;
}
System.out.println("max=" +max +"\nmaxidx="+maxidx);
}
}
![]()
package com.homework8;
//6、将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)
public class Exe8_2_1 {
public static void main(String[] args) {
int a[] =new int[10];
for (int i = 0; i < a.length; i++) {
a[i] =i;
}
for (int i : a) {
System.out.print(i +" ");
}
System.out.println();
for (int i = 0; i < a.length; i++) {
for (int j = i; j < a.length; j++) {
int temp =a[i];
a[i] =a[j];
a[j] =temp;
}
}
for (int i : a) {
System.out.print(i +" ");
}
}
}
![]()
package com.homework8;
/*7. 将一个数组中的重复元素保留一个其他的清零。
(知识点:数组遍历、数组元素访问)*/
public class Exe8_2_2 {
public static void main(String[] args) {
int[] a ={1,2,2,3,4,4,5,5,6,6,6,7,7,8,8,4,1};
for (int i : a) {
System.out.print(i +"\t");
}
System.out.println();
for (int i = 0; i < a.length; i++) {
for (int j = i+1; j < a.length; j++) {
if (a[i] == a[j])
a[j] =0;
}
}
for (int i : a) {
System.out.print(i +"\t");
}
}
}
![]()
package com.homework8;
/*8、给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的
平均值、最大值、最小值。(知识点:数组遍历、数组元素访问)*/
public class Exe8_2_3 {
public static void main(String[] args) {
int a[] =new int[]{-10,2,3,246,-100,0,5};
int sum = 0;
int max =a[0];
int min =a[0];
for (int i = 0; i < a.length; i++) {
if (max < a[i]) {
max = a[i];
}
if (min > a[i]) {
min = a[i];
}
sum +=a[i];
}
System.out.println("最大值是" +max);
System.out.println("最小值是" +min);
System.out.println("平均值是:" +sum*1.0/a.length);
}
}
![]()
package com.homework8;
//9、使用数组存放裴波那契数列的前20项 ,并输出 1 1 2 3 5 8 13 21
public class Exe8_2_4 {
public static void main(String[] args) {
int a[] =new int [20];
a[0] = 1;
a[1] = 1;
for (int i = 2; i < a.length; i++) {
a[i] =a[i-1] +a[i-2];
}
for (int i : a) {
System.out.print(i +" ");
}
}
}
![]()
package com.homework8;
import java.util.Arrays;
import java.util.Random;
/*10、生成一个长度为10的随机整数数组(每个数都是0-100之间),
输出,排序后,再输出*/
public class Exe8_2_5 {
public static void main(String[] args) {
Random r =new Random();//生成随机数
int a[] =new int[10];
for (int i = 0; i < a.length; i++) {
a[i] =r.nextInt(100);
}
Arrays.sort(a);//Arrays.sort排序及foreach遍历
for (int i : a) {
System.out.print(i +"\t");
}
System.out.println();//冒泡排序
for (int i = 0; i < a.length; i++) {
for (int j = i; j < a.length; j++) {
if (a[i] < a[j]) {
int temp =a[i];
a[i] =a[j];
a[j] =temp;
}
}
}
for (int i : a) {
System.out.print(i +"\t");
}
}
}
![]()