# 2020.1.16 Java数组基础

## 1. Arrays.toString( )

直接输出数组是显示不出来的，要调用Arrays.toString()方法将其转换为字符串再输出。

import java.util.Arrays;
public class E {
public static void main(String[] args) {
int[] num= {23,34,6,2,31,9};
System.out.println(Arrays.toString(num));
}
}


## 2. Arrays.equals( )

import java.util.Arrays;

public class C {
public static void main(String[] args) {
String[] one= {"A","3","FDF","65","abc"};
String[] two= {"B","DF","89","ahg","9"};
String[] three= {"A","3","FDF","65","abc"};
boolean is=Arrays.equals(one, three);
boolean is2=Arrays.equals(one, two);
System.out.println("one和two的比较结果为:"+is);
System.out.println("onr和three的比较结果为:"+is2);
}
}


## 3. Arrays.sory( )

import java.util.Arrays;

public class D {
public static void main(String[] args) {
int[] number= {4,3,24,35,622,2,56,31,8,46};
System.out.println("排序前:"+Arrays.toString(number));
Arrays.sort(number);
System.out.println("排序后:"+Arrays.toString(number));
}
}


## 4. Arrays.fill( )

import java.util.Arrays;

public class E {
public static void main(String[] args) {
int[] num=new int[100];
System.out.println("填充前:"+Arrays.toString(num));
Arrays.fill(num, 9);
System.out.println("填充后:"+Arrays.toString(num));
}
}


## 5. Arrays.copyOf( )，copyOfRange( )和System.arraycopy( )

System.arraycopy(被复制的数组，复制的开始位置，目标数组，复制到目标数组的那个位置，要复制的个数)

import java.util.Arrays;

public class F {
public static void main(String[] args) {
String[] number={"李敏镐","肖战","迪丽热巴","陈飞宇","吴亦凡","王一博","朱亚文"};
System.out.println(Arrays.toString(number));
String[] newnum=new String[5];
//Arrays.copyOf
newnum=Arrays.copyOf(number, 5);
System.out.println("复制后:"+Arrays.toString(newnum));
//Arrays.copyOfRange
newnum=Arrays.copyOfRange(number, 1, 6);
System.out.println("复制下标1至5之间的元素:"+Arrays.toString(newnum));
//System.arraycopy
System.arraycopy(number, 1, newnum, 0, 5);
System.out.println(Arrays.toString(newnum));

}
}


# 运行结果

## 6. 练习

1. 洗牌

import java.util.Arrays;
import java.util.Random;

public class A {
public static void main(String[] args) {
String[] pie= {"♥A","♠A","✿A","♦A","♥2","♠2","✿2","♦2","♥3","♠3","✿3","♦3","♥4","♠4","✿4","♦4"};
System.out.println("洗牌前:");
System.out.println(Arrays.toString(pie));
Random ran=new Random();
for(int i=pie.length-1;i>=1;i--) {
int index=ran.nextInt(i);
String temp=pie[i];
pie[i]=pie[index];
pie[index]=temp;
}
System.out.println("洗牌后:");
System.out.println(Arrays.toString(pie));
}
}


## 运行结果

2. 产生扑克的所有花色

import java.util.Arrays;

public class B {
public static void main(String[] args) {
String[] number= {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String[] puk= {"♥","♠","✿","♦"};
String[] newpuk=new String[number.length*puk.length];
int i=0;
for(String num:number) {
for(String pukk:puk) {
newpuk[i]=pukk+num;
i++;
}
}
System.out.println(Arrays.toString(newpuk));
}
}


## 运行结果

3. 抽奖一百次，统计每个人被抽到的次数放入数组，输出最多的那个人

import java.util.Arrays;
import java.util.Random;

public class A {
public static void main(String[] args) {
Random ran=new Random();
String[] lucky= {"李敏镐","肖战","迪丽热巴","陈飞宇","吴亦凡","王一博","朱亚文","王嘉尔","宋祖儿","胡一天"};
int[] counts=new int[lucky.length];
for(int i=0;i<100;i++) {
int index=ran.nextInt(lucky.length);
counts[index]++;
}
System.out.println(Arrays.toString(counts));
//最大次数max
int max=counts[0];
//最大次数的下标
int maxindex=0;
for(int j=1;j<counts.length;j++) {
if(counts[j]>max) {
max=counts[j];
maxindex=j;
}
}
System.out.println("最大次数为:"+max);
System.out.println("下标为:"+maxindex);
System.out.println("神赐之人为:"+lucky[maxindex]);
}
}


## 运行结果

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试