java基础--数组--练习集锦

2018/04/24 20:32
阅读数 66

##数组反转

package zsc.czy.array;

public class A {
	public static void main(String[] args) {
  //最笨的方法
		int a[] = new int[5];
		for(int i = 0;i<a.length;i++){
			a[i] = (int) (Math.random() * 100);
			System.out.println(a[i]);
		}
		System.out.println();
		int b[] = new int[5];
		for (int i = 0; i < b.length; i++) {
			b[i]= a[a.length-i-1];
			System.out.println(b[i]);
		}
	}
}

package zsc.czy.array;

public class B {
        //首尾交换
	public static void main(String[] args) {
		int a[] = new int[5];
		for(int i = 0;i<a.length;i++){
			a[i] = (int) (Math.random() * 100);
			System.out.println(a[i]);
		}
		System.out.println();
		int temp =0;
		for (int i = 0; i < a.length/2; i++) {
			 temp = a[i];
			 a[i]= a[a.length-1-i];
			 a[a.length-1-i] = temp;
		}
		for(int i :a){
			System.out.println(i);
		}
	}

}

##选择排序 一开始我的错误写法

package zsc.czy.arraySort;

public class QuickSort {
/**
 *我第一次写,是这样的,是错误的,但还不知道错在哪里
 * 
 * @param args
 */
	public static void main(String[] args) {
		int a[] = new int[5];
		for (int i = 0; i < a.length; i++) {
			a[i] = (int) (Math.random() * 100);
			System.out.println(a[i]);
		}
		System.out.println();
		int temp=0;
		for (int i = 0; i < a.length-1; i++) {
			for(int j = i;j<a.length-1;j++){
				if(a[j]>a[j+1]){
					temp = a[j];
					a[j]=a[j+1];
					a[j+1]=temp;
				}
			}
		}
		for(int i:a){
			System.out.println(i);
		}
	}

}

###正确做法

package zsc.czy.arraySort;

public class QuickSort {
/**
 * 选择法排序的思路: 
把第一位和其他所有的进行比较,只要比第一位小的,就换到第一个位置来 
比较完后,第一位就是最小的 
然后再从第二位和剩余的其他所有进行比较,只要比第二位小,就换到第二个位置来 
比较完后,第二位就是第二小的 
以此类推
 * 
 * @param args
 */
	public static void main(String[] args) {
		int a[] = new int[5];
		for (int i = 0; i < a.length; i++) {
			a[i] = (int) (Math.random() * 100);
			System.out.println(a[i]);
		}
		System.out.println();
		int temp=0;
		for (int i = 0; i < a.length-1; i++) {
			for(int j = i+1;j<a.length;j++){
				if(a[j]<a[i]){
					temp = a[i];
					a[i]=a[j];
					a[j]=temp;
				}
			}
		}
		for(int i:a){
			System.out.println(i);
		}
	}

}

##冒泡排序

package zsc.czy.arraySort;

public class BubbleSort {
/*
 * 冒泡法排序的思路: 
 * 第一步:从第一位开始,把相邻两位进行比较 
 * 如果发现前面的比后面的大,就把大的数据交换在后面,循环比较完毕后,最后一位就是最大的
 * 第二步: 再来一次,只不过不用比较最后一位 
 * 以此类推
 */
	public static void main(String[] args) {
		int a[] = new int[5];
		for (int i = 0; i < a.length; i++) {
			a[i] = (int) (Math.random() * 100);
			System.out.println(a[i]);
		}
		System.out.println();
		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 i :a){
			System.out.println(i);
		}
	}

}

##合并数组 ###题目 首先准备两个数组,他俩的长度是5-10之间的随机数,并使用随机数初始化这两个数组 然后准备第三个数组,第三个数组的长度是前两个的和 通过System.arraycopy 把前两个数组合并到第三个数组中

package zsc.czy.HebingArray;

public class HeBing {
	public static void main(String[] args) {
		int a[] = new int[5];
		int b[] = new int[5];
		for (int i = 0; i < a.length; i++) {
			a[i] = (int) (Math.random() * 100);
			b[i] = (int) (Math.random() * 100);

			System.out.println(a[i]);
			
		}
		System.out.println();
		for(int bb :b){
			System.out.println(bb);
		}
		System.out.println();
		int c[] = new int[a.length+b.length];
		System.arraycopy(a, 0, c, 0, a.length);
		System.arraycopy(b, 0, c, a.length, b.length);  ///从下表c[5]开始
		for(int cc :c){
			System.out.println(cc);
		}
	}
}

##二维数组 定义一个5X5的二维数组。 然后使用随机数填充该二维数组。 找出这个二维数组里,最大的那个值,并打印出其二维坐标

package zsc.czy.erWeiArray;

public class A {

	public static void main(String[] args) {
		int a[][] = new int[5][5];
		
		for(int i=0;i<a.length;i++){
			for(int j=0;j<a[i].length;j++){
				a[i][j]= (int)(Math.random()*100);
			}
		}
		
		  // 打印这个数组的内容:
        for (int[] row : a) {
            for (int each : row) {
                System.out.print(each + "\t");
            }
            System.out.println();
        }
        
		int max=a[0][0];
		int temp;
		for(int i=0;i<a.length;i++){
			for(int j=0;j<5;j++){
				if(a[i][j]>max){
					max = a[i][j];
				}
			}
			
		}
		System.out.println(max);
	}

}

##二位数组排序 首先定义一个5X8的二维数组,然后使用随机数填充满。 借助Arrays的方法对二维数组进行排序。

参考思路: 先把二维数组使用System.arraycopy进行数组复制到一个一维数组 然后使用sort进行排序 最后再复制回到二维数组。

package zsc.czy.ArrayUtil;

import java.util.Arrays;

public class ErWeiArraySort {

	public static void main(String[] args) {
		int a[][] = new int[5][8];
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				a[i][j] = (int) (Math.random() * 100);
			}
		}
		// System.out.println(Arrays.toString(a));
		for (int aa[] : a) {
			for (int aaa : aa) {
				System.out.print(aaa + "\t");
			}
			System.out.println();
		}

		int newArray[] = new int[a.length * a[0].length];
//		for (int row[] : a) {
//			System.arraycopy(row, 0, newArray, 0, row.length);
//			System.out.println(Arrays.toString(newArray));
//		}
		for(int i=0;i<a.length;i++){
			//包里不包外
		System.arraycopy(a[i], 0, newArray, a[i].length*i, a[i].length);
		}
		System.out.println(Arrays.toString(newArray));
		Arrays.sort(newArray);
		System.out.println();
		System.out.println(Arrays.toString(newArray));
	}

}

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部