java.util.Arrays类学习

package shenhui;
import java.util.Arrays;
//util.Arrays类学习
public class code12{
	public static void main(String[] args){
		int score[]=new int[20];
		int scorebak[]=new int[score.length];
		System.out.println("score数组未排序前元素为:");
		for(int a=0;a<score.length;a++){
			score[a]=(int)(Math.random()*100);
			//使用Math类产生double类型的随机数,然后强制转型;0≤随机数<1;
		}
		System.arraycopy(score,0,scorebak,0,score.length);
		//使用arraycopy方法复制数组(原数组,要复制的原数组开始值,要粘贴的数组,从待粘贴数组何处粘贴,复制的长度)
		System.out.println(Arrays.toString(score));
		//Arrays.toString方法打印出数组元素
		//————————java.util.Arrays.sort()————————————————————//
		Arrays.sort(score);
		//使用Arrays.sort()方法进行升序排列
		System.out.println("使用arrays.sort方法进行排序后数组为:");
		System.out.println(Arrays.toString(score));
		System.out.println("arrays.sort方法可以带范围进行排序:");
		Arrays.sort(scorebak,0,10);
		//Arrays.sort可以只排序指定范围的索引(要排序的数组名,开始点,结束点)
		System.out.println(Arrays.toString(scorebak));
		//————————java.uil.Arrays.hashCode()——————————————————————//
		int hashscore=score.hashCode();
		System.out.println("数组score的哈希值是:"+hashscore);
		int hashscorebak=scorebak.hashCode();
		System.out.println("数组scorebak的哈希值是:"+hashscorebak);
		//——————————java.util.Arrays.fill()——————————————————//
		Arrays.fill(score, 333);
		//使用Arrays.fill方法替换掉score数组中所有元素
		System.out.println("被替换后的score数组内容为:");
		System.out.println(Arrays.toString(score));
		Arrays.fill(score, 0,10,222);
		//当然Arrays.fill方法可以定义替换起止索引(数组名,开始索引,结束索引,要替换的内容)
		System.out.println("重新部分替换后的score为:");
		System.out.println(Arrays.toString(score));
		//——————————java.util.Arrays.equals()———————//
		boolean a =Arrays.equals(score, scorebak);
		//数组要元素个数与内容完全一致才相等
		if(a==true){
			System.out.println("数组score与数组scorebak相等");
		}
		else
			System.out.println("数组score不等于数组scorebak");
		
	}
}

猜你喜欢

转载自shenhuibad.iteye.com/blog/2211998