简体   繁体   English

将1D数组转换为2D数组而不重复

[英]Converting 1D array to 2D array without duplicates

I am attempting to convert a 1D array into a 2D array([5][5]) without it using duplicate characters. 我正在尝试将一维数组转换为二维数组([5] [5]),而无需使用重复字符。 I can't seem to get it to be consistent, sometimes it works and sometimes it doesn't. 我似乎无法使它保持一致,有时它可以工作,有时却不能。

Input: billys 输入:比利

Output: [[b, i, l, y, ], [ , , , , ], [ , , , , ], [ , , , , ], [ , , , , ]] 输出:[[b,i,l,y,],[,,,,],[,,,,],[,,,,],[,,,,]]

Obviously missing the 's' but has correctly removed one of the 'l's 显然缺少“ s”,但已正确删除了“ l”中的一个

Input: billysb 输入:billysb

output: [[b, i, l, y, s], [ , , , , ], [ , , , , ], [ , , , , ], [ , , , , ]] 输出:[[b,i,l,y,s],[,,,,],[,,,,],[,,,,],[,,,,]]

Again, correctly removed the duplicate letters. 再次,正确删除重复的字母。

Input: billysabc 输入:billysabc

output: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9 输出:线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:9

Input: bbbbbbb 输入:bbbbbbb

Ouput: [[b, b, b, , ], [ , , , , ], [ , , , , ], [ , , , , ], [ , , , , ]] 输出:[[b,b,b,,],[,,,,],[,,,,],[,,,,],[,,,,]]

Removing some, but not all duplicates. 删除一些但不是全部重复项。

here's the code doing the conversion for me: 这是为我执行转换的代码:

 char[][] playFairGrid = new char[5][5]; //write keyword to array char[] keywordBreakdown = keyword.toCharArray(); //write keyword to grid for(int i =0; i<5;i++){ for(int j=0;j<5;j++){ if (count==keywordBreakdown.length-1) break; //check for duplicates for(int m =0; m<5;m++){ for(int l=0;l<5;l++){ if (keywordBreakdown[count]==playFairGrid[m][l]){ count++; } } } playFairGrid[i][j]=keywordBreakdown[count]; count++; } } System.out.println(Arrays.deepToString(playFairGrid)); 

all help greatly appreciated! 所有帮助,不胜感激! this has been a pain for a few hours now. 这已经好几个小时了。

如果要创建没有重复项的集合,则应使用Set Collection

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM