所以这是代码,我显示了5的倍数,然后将其随机播放。
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class TwoDimensionalArrays {
public static void main(String[] args) {
此部分显示5个数字的整数,最大为500。
int [][]table = new int[10][10];
int x = 5;
for(int i = 0; i < table.length; i++){
for(int j=0; j < table[i].length; j++){
table[i][j]= x;
x+=5;
System.out.print(table[i][j] + "\t");
}
System.out.println();
}
这部分是使用Math.random的随机数组。
System.out.println("\nShuffled Arrays: \n");
int index1 = 0;
for(int a = 0; a < table.length; a++) {
for(int b = 0; b < table[a].length; b++) {
int il = (int)(Math.random()*table.length);
int jl = (int)(Math.random()*table[a].length);
int temp = table[a][b];
table[a][b] = table[il][jl];
table[il][jl] = temp;
System.out.print(table[a][b] + "\t");
}
System.out.println();
}
}
}
我如何从经过改组的数组中打印元素的索引号,例如40?
请检查以下答案。在这里,我添加了单独的for
循环来打印经过改组的数组。因为在打印table[a][b]
值后的当前实现中,该值可以用随机生成的索引替换。因此,最好的方法是在完全重组后打印重组后的数组。用Map<String, Integer>
来保留带有值的索引。请检查以下代码:
public static void main(String[] args) {
int[][] table = new int[10][10];
int x = 5;
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
table[i][j] = x;
x += 5;
System.out.print(table[i][j] + "\t");
}
System.out.println();
}
System.out.println("\nShuffled Arrays: \n");
Map<String, Integer> map = new HashMap<>(); //Hash map to keep indexes
//Shuffle the array
for (int a = 0; a < table.length; a++) {
for (int b = 0; b < table[a].length; b++) {
int il = (int) (Math.random() * table.length);
int jl = (int) (Math.random() * table[a].length);
int temp = table[a][b];
table[a][b] = table[il][jl];
table[il][jl] = temp;
}
}
//Print shuffled array
for (int a = 0; a < table.length; a++) {
for (int b = 0; b < table[a].length; b++) {
int value = table[a][b];
System.out.print(value + "\t");
//Insert indexes to hash map as key value pairs
String key = a + ", " + b;
if (value == 40 || value == 320 || value == 450) {
map.put(key, value);
}
}
System.out.println();
}
//Printing indexes
System.out.println("\nIndexes: \n");
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getValue() + " [" + entry.getKey() + "]");
}
}
所以这里的想法是检查随机数组中是否存在随机索引的值(在改组之前),如果是,则将随机条目添加到值中作为随机数组中新分配的索引的映射中,因此一旦改组后,这种方式数组已经准备好了,您已经完成了映射,其中每个值的所有详细信息及其索引都在随机数组中
public static void main(String [] args) {
int [][]table = new int[10][10];
Map<Integer,String> map = new HashMap<>();
int x = 5;
for(int i = 0; i < table.length; i++){
for(int j=0; j < table[i].length; j++){
table[i][j]= x;
x+=5;
System.out.print(table[i][j] + "\t");
}
System.out.println();
}
System.out.println("\nShuffled Arrays: \n");
int index1 = 0;
for(int a = 0; a < table.length; a++) {
for(int b = 0; b < table[a].length; b++) {
int il = (int)(Math.random()*table.length);
int jl = (int)(Math.random()*table[a].length);
int temp = table[a][b];
if(exists(table[il][jl], table)) {
map.put(table[il][jl], "["+a + "][" + b + "]");
}
table[a][b] = table[il][jl];
table[il][jl] = temp;
System.out.print(table[a][b] + "\t");
}
System.out.println();
}
System.out.println(map);
}
public static boolean exists(int value, int[][] tmp) {
List<int[]> list = Arrays.asList(tmp);
for(int[] arr: list){
if(Arrays.stream(arr).anyMatch(i -> i == value)) {
return true;
}
}
return false;
}
希望这会有所帮助.. !!
请检查以下答案。在这里,我添加了单独的for
循环来打印经过改组的数组。因为在打印table[a][b]
值后的当前实现中,该值可以用随机生成的索引替换。因此,最好的方法是在完全重组后打印重组后的数组。用Map<String, Integer>
来保留带有值的索引。请检查以下代码:
public static void main(String[] args) {
int[][] table = new int[10][10];
int x = 5;
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
table[i][j] = x;
x += 5;
System.out.print(table[i][j] + "\t");
}
System.out.println();
}
System.out.println("\nShuffled Arrays: \n");
Map<String, Integer> map = new HashMap<>(); //Hash map to keep indexes
//Shuffle the array
for (int a = 0; a < table.length; a++) {
for (int b = 0; b < table[a].length; b++) {
int il = (int) (Math.random() * table.length);
int jl = (int) (Math.random() * table[a].length);
int temp = table[a][b];
table[a][b] = table[il][jl];
table[il][jl] = temp;
}
}
//Print shuffled array
for (int a = 0; a < table.length; a++) {
for (int b = 0; b < table[a].length; b++) {
int value = table[a][b];
System.out.print(value + "\t");
//Insert indexes to hash map as key value pairs
String key = a + ", " + b;
if (value == 40 || value == 320 || value == 450) {
map.put(key, value);
}
}
System.out.println();
}
//Printing indexes
System.out.println("\nIndexes: \n");
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getValue() + " [" + entry.getKey() + "]");
}
}