第八周
1、编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)[必做题]?
package zxcvbnm;
public class asdfghjkl {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[]=new int[]{10,20,30,40,50};
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]+"");
}
}
}
2、将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。(知识点:数组复制) [必做题]?
package zxcvbnm;
public class asdfghjkl {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String []a={"neusofteducation"};
String []b=new String[1];
for (int i = 0; i < a.length; i++) {
b[i]=a[i];
System.out.print(b[i]+"");
}
}
}
3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序)
package zxcvbnm;
public class asdfghjkl {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[]a={1,6,2,3,9,4,5,7,8};
int temp;
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]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for (int n:a) {
System.out.print(n+" ");
}
}
}
4、 输出一个double型二维数组(长度分别为5、4,值自己设定)的值。(知识点:数组定义和创建、多维数组初始化、数组遍历)
package zxcvbnm;
public class asdfghjkl {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
double [][]a2=new double[5][4];
for (int i = 0; i < a2.length; i++) {
for (int j = 0; j < a2[i].length; j++) {
a2[i][j]=i+j;
}
}
for (int i = 0; i < a2.length; i++) {
for (int j = 0; j < a2[i].length; j++) {
System.out.println(a2[i][j]);
}
System.out.println();
}
}
}
5、 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问) [必做题]?
package zxcvbnm;
public class asdfghjkl {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int []a={18,25,7,36,13,2,89,63};
int max=a[0],maxid=0;
for (int i = 1; i < a.length; i++) {
if (max<a[i]) {
max=a[i];
maxid=i;
}
}
System.out.println("最大数为"+max+"下标为"+maxid);
}
}
6.
package jsadf;
public class Test1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[]={20,4,1990};
for (int i = 0; i < a.length/2; i++) {
int b;
b=a[i];
a[i]=a[a.length-1-i];
a[a.length-1-i]=b;
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
}
}
7.
package zyesdz;
public class Test2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = { 1, 2, 2, 3, 5, 4, 5 };
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if (a[i] == a[j] && i != j) {
a[j] = 0;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
8.package jtzy426;
public class Test3 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[] = { -10, 2, 3, 246, -100, 0, 5 };
double p = 0;
double sum = 0;
int max = a[0];
int min = a[0];
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
p = sum / a.length;
for (int i = 1; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
}
if (a[i] < min) {
min = a[i];
}
}
System.out.println("平均数是" + p + "\n" + "最大值是" + max + "\n"
+ "最小值是" + min);
}
}
9.package jtzy426;
public class Test4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
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 = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
10.
package jgsfg;
import java.util.Arrays;
import java.util.Random;
public class Test5 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int []a=new int[10];
Random r=new Random();
for (int i = 0; i < a.length; i++) {
a[i]=r.nextInt(101);
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
System.out.println("\n"+"重新排序后");
Arrays.sort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
}
}