数组_参考答案.docVIP

  • 297
  • 0
  • 约8.48千字
  • 约 10页
  • 2016-06-07 发布于湖北
  • 举报
一、【必做题】 1. 编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。 /*例5-1 *数组使用范例 */ public class ArrayDemo { public static void main(String[] args) { int[] buffer=new int[5]; buffer[0]=10; buffer[1]=20; buffer[2]=30; buffer[3]=40; buffer[4]=50; for(int i=0;i5;i++) { System.out.println(buffer[i]); } } } 2. 输出一个double型二维数组(长度分别为5、4,值自己设定)的值。 /*例5-3 *多维数组范例 */ public class ArrayTwoDimension { public static void main(String[] args) { double[][] buffer=new double[5][4]; for(int i=0;ibuffer.length;i++) { for(int j=0;jbuffer[0].length;j++) { System.out.print(buffer[i][j]); } System.out.println(); } } } 3.将一个字符数组的值(neusofteducation)考贝到另一个字符数组中。 public class ArrayCopyDemo { public static void main(String[ ] args) { //定义源字符数组 char[ ] copyFrom = {n, e, u, s, o, f, t, e, d, u, c, a, t, i, o, n}; char[ ] copyTo = new char[7]; System.arraycopy(copyFrom, 2, copyTo, 0, 7); System.out.println(new String(copyTo)); } } 4. 给定一个有9个整数(1,6,2,3,9,4,5,7,8})的数组,先排序,然后输出排序后的数组的值。 public class ArraySortDemo { /** * @param args */ 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;ipoint.length;i++) { System.out.println(point[i]); } } } 5. 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。 public class Arraymax { /** * @param args */ public static void main(String[] args) { int[] a = {18,25,7,36,13,2,89,63}; int max = a[0]; int maxIndex = 0; for(int i=1;ia.length;i++) { if(max=a[i]){ max = a[i]; maxIndex = i; } } System.out.println(最大值为:+max+ 最大值下标为:+maxIndex); } } 6、有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。import java.util.Scanner; public class Ar

文档评论(0)

1亿VIP精品文档

相关文档