Fla*_*lyn 2 java arrays shuffle
我正在尝试编写一种简单的加密算法,即随机移位模式加密。我将其编写为基于所有字符的数组,然后使用Collections.shuffle然后将其与之匹配。但是,数组似乎没有改组,因为输出文本与输入相同
这是我的方法
static void encrypt(String s)
{
//Define variable for indexOf output
int n;
//Encrypted output
String output = "";
//Shuffle array to create random replacements
Collections.shuffle(Arrays.asList(alphashuff));
//Create new string
String alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int index = 0; index < s.length();index++)
{
char aChar = s.charAt(index);
n = alpha.indexOf(aChar, 0);
if(n == -1)
{
output = output + aChar;
}
else
{
output = output + alphashuff[n];
}
}
//Print encrypted string to console
System.out.println("Encrypted Text: " + output);
}
Run Code Online (Sandbox Code Playgroud)
您不是在改组数组,而是在使用数组创建的列表。创建列表,然后将其发送到随机播放:
//I'm assuming alphashuff is a char[]
List<Character> lstCh = new ArrayList<Character>();
for(char c : arrCh) {
lstCh.add(c);
}
Collections.shuffle(lstCh);
//...
else
{
output = output + lstCh.get(n);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2347 次 |
| 最近记录: |