Java数组和for循环导致错误输出
要求检查数组中的重复项并将其删除,然后数组中的其余项必须向左移动 我写道:Java数组和for循环导致错误输出,java,arrays,Java,Arrays,要求检查数组中的重复项并将其删除,然后数组中的其余项必须向左移动 我写道: public class TestRepeat { public static int deleteRepeats(char[] ch) { int count = 0; for (int x = 0; x < ch.length; x++) { for (int y = x + 1; y < ch.length; y++) { if (ch[x]
public class TestRepeat {
public static int deleteRepeats(char[] ch) {
int count = 0;
for (int x = 0; x < ch.length; x++) {
for (int y = x + 1; y < ch.length; y++) {
if (ch[x] == ch[y]) {
for (int k = y; k < ch.length - 1; k++) {//shifts the array to the right if its a duplicate
ch[k] = ch[k + 1];
}
ch[ch.length - 1] = ' ';//replaces the last array with a blank
}
}
}
for (int q = 0; q < ch.length; q++) {
if (ch[q] == ' ') {
count++;
}
}
return count;//returns the number of deleted items
}
public static void main(String[] args) {
char[] ch = {'k', 'a', 'm', 'o', 'k', 'm', 'y', 'm', 'k', 'k', 'x', 'm', 'm', 'o'};
System.out.print("The original array is: ");
System.out.println(ch);
System.out.println("Number of deleted characters: " + deleteRepeats(ch));
System.out.print("The new array is: ");
System.out.println(ch);
}
}
公共类TestRepeat{
公共静态int-repeats(char[]ch){
整数计数=0;
对于(int x=0;x
它应该返回:
原始数组是:kamokmymkxmmo
已删除字符数:8
新阵列是:kamoyx
但相反,它正在回归:
原始数组是:kamokmymkxmmo
已删除字符数:6
新阵列是:kamoykxm
导致问题的原因是什么?如何解决 我发现了两个错误,一个是导致您出现问题的原因,另一个是必然结果。首先,不能将for循环用于内部循环,因为有时您要修改正在循环的数组。因此,在某些迭代中,您将隐式地增加y两次:一次是通过实际增加y,另一次是当您将数组的部分向左移动时。因此,只有在不对数组执行更改时,才应该实际递增y,而在删除元素时将y保留在原来的位置。其次,您必须确保不要尝试删除
'
,因为这将在while循环版本中导致无限递归。错误修复如下:
public class TestRepeat {
/** Deletes index index in arr. Elements in (index, arr.length) are shifted to the left,
And a ' ' is put at the end of arr
Precondition: index >= 0, index < arr.length */
private static void deleteAndShift(char[] arr, int index){
for(int i = index; i < arr.length - 1; i++){
arr[i] = arr[i+1];
}
arr[arr.length - 1] = ' ';
}
public static int deleteRepeats(char[] ch) {
int count = 0;
for (int x = 0; x < ch.length; x++) {
int y = x+1;
while(y < ch.length){
//Delete index y. Note that this 'implicitly' increments y by shifting ch.
if (ch[x] != ' ' && ch[x] == ch[y]) {
deleteAndShift(ch, y);
}
//Only increment y if an element wasn't deleted
else{
y++;
}
}
}
for (int q = 0; q < ch.length; q++) {
if (ch[q] == ' ') {
count++;
}
}
return count;//returns the number of deleted items
}
public static void main(String[] args) {
char[] ch = {'k', 'a', 'm', 'o', 'k', 'm', 'y', 'm', 'k', 'k', 'x', 'm', 'm', 'o'};
System.out.print("The original array is: ");
System.out.println(ch);
System.out.println("Number of deleted characters: " + deleteRepeats(ch));
System.out.print("The new array is: ");
System.out.println(ch);
}
}
公共类TestRepeat{
/**删除arr中的索引。将(index,arr.length)中的元素向左移动,
并且在arr的末尾放一个“”
前提条件:索引>=0,索引
此代码具有删除示例输入中8个字符的所需输出。将
if
条件块if(ch[x]==ch[y]){
替换为
if (ch[x] != ' ' && ch[x] == ch[y]) {
for (int k = y; k < ch.length - 1; k++) {//shifts the array to the right if its a duplicate
ch[k] = ch[k + 1];
}
ch[ch.length - 1] = ' ';//replaces the last array with a blank
y--;
}
if(ch[x]!=''&&ch[x]==ch[y]){
对于(int k=y;k
将其向左移动后,执行y--;
这将有助于处理连续重复的元素借助于我们可以轻松地实现它
char[] ch = {'k', 'a', 'm', 'o', 'k', 'm', 'y', 'm', 'k', 'k', 'x', 'm', 'm', 'o'};
System.out.print("The original array is : ");
System.out.println(ch);
// Moving in to LinkedHashSet
Set<Character> charSet = new LinkedHashSet<Character>();
for(char c : ch)
charSet.add(c);
System.out.println("Number of deleted characters :"+(ch.length-charSet.size()));
// Move Back to newArray
char[] newch = new char[charSet.size()];
int i = 0;
for(char c : charSet)
newch[i++] = c;
System.out.print("The new array is :");
System.out.println(newch);
这可能会有帮助:stackoverflow不是正确的门户,请在以下网站上询问:@NoobEditor为什么这不是正确的门户?他有一个编程错误,他不知道如何修复。非常感谢您的帮助更改for循环中for循环索引变量的值不是很好的做法。@Mshnik可以更改控件l很好,
y++
或y--
在循环中。
The original array is : kamokmymkkxmmo
Number of deleted characters :8
The new array is :kamoyx