标签:void author next import ash 大小 dem new pac
package arr;
import java.util.Random;
/**
@author Christie2020
*/
public class HashDemo {
public static void main(String[] args) {
//制作一个测试数组
int[] testArr = new int[1000];
Random random = new Random();
int[] supportArr = new int[50];
for (int i = 0; i < supportArr.length; i++) {
supportArr[i] = random.nextInt(9999);
}
//输入预设置的50个数
System.out.println("输出预设置的50个数");
for (int i = 0; i < supportArr.length; i++) {
if ((i+1)%5 == 0 && i != 0){
System.out.printf(String.format("%4d\n",supportArr[i]));
}else {
System.out.printf(String.format("%4d\t",supportArr[i]));
}
}
for (int i = 0; i < 50; i++) {
testArr[random.nextInt(1000)] = supportArr[i];
}
HashArray hashArray = new HashArray();
hashArray.hashArr(testArr);
}
}
class HashArray{
//输入一个大小1000的数组
public void hashArr(int[] arr){
//创建容器数组
int[] targetArr = new int[100];
//
int temp = 0;
for (int i = 1; i < 1000; i++) {
temp = arr[i]%100;
if (arr[i] != 0){
if (targetArr[temp] == 0){ //如果当前位置为0,则直接存储
}else {//如果当前位置不为0,则顺序向下找
while (targetArr[temp] != 0 ){
if (temp <100){
temp++;
}else {
temp = 0;
}
}
}
targetArr[temp] = arr[i];
}
}
//输出变化后的数组
System.out.println("输出变化后的数组");
for (int i = 0; i < targetArr.length; i++) {
if ((i+1)%10 == 0 && i != 0){
System.out.printf(String.format("%4d\n",targetArr[i]));
}else {
System.out.printf(String.format("%4d\t",targetArr[i]));
}
}
}
}
标签:void author next import ash 大小 dem new pac
原文地址:https://www.cnblogs.com/lms2020/p/12374178.html