我试图制造一个阵列,除空间外,删除重复之处(“”)。 我看到了一些在线方法,但我不想复制。 我尝试了许多方法,但我不理解我的方法为什么不奏效。
这是我所尝试的:
在第一个方法中,该方法是接收投入、打电话、制作一个阵列清单、通过插图打字,同时在阵列清单中增加每个特性,第二次通过清单将每个特性与第一个指数的特性进行比较,同时检查这些特性是否平等,然后从阵列清单中删除。
问题似乎是“在平等后将其从阵列清单中删除”:
if(character.get(i).equals(character.get(k))) {
character.remove(arr.charAt(i));
}
第二次
for(int k = 0;k < characters.length;k++) {
if(characters[i]==characters[k]) {
System.out.print(character);
character.remove(characters[i]);
}
import java.util.*;
public class NoRepeats {
public static void main(String args[]) {
System.out.print("Please enter a phrase: ");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
char[] characters = chars(input);
}
public static char[] chars(String arr) {
ArrayList<Character> character = new ArrayList<Character>();
for(int i = 0; i<arr.length();i++) {
character.add(arr.charAt(i));
for(int k = 1; k<arr.length();k++) {
if(character.get(i).equals(character.get(k))) {
character.remove(arr.charAt(i));
}
}
}
System.out.print(character);
return null;
}
}
import java.util.*;
public class NoRepeats {
public static void main(String args[]) {
System.out.print("Please enter a phrase: ");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
//receive input
char[] characters = chars(input);
//call function
}
public static char[] chars(String arr) {
ArrayList<Character> character = new ArrayList<Character>();
//create an empty arraylist
char[] characters = arr.toCharArray();
for(int j = 0; j<characters.length;j++) {
character.add(characters[j]);
}
for(int i = 0; i<characters.length;i++) {
/*traverse through the orignal array adding a character to
the empty array list each time*/
for(int k = 0;k < characters.length;k++) {
if(characters[i]==characters[k]) {
System.out.print(character);
character.remove(characters[i]);
}
}
}
return null;
}
}
预期投入:
“And I think to myself: what a wonderful world!”
预期产出:
And I thk o myself: w ru !
第一个错误是:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266) at java.base/java.util.Objects.checkIndex(Objects.java:359) at java.base/java.util.ArrayList.get(ArrayList.java:427) at NoRepeats.chars(NoRepeats.java:20) at NoRepeats.main(NoRepeats.java:10)
第二种错误是:
"Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 118 out of bounds for length 2 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266) at java.base/java.util.Objects.checkIndex(Objects.java:359) at java.base/java.util.ArrayList.remove(ArrayList.java:504) at NoRepeats.chars(NoRepeats.java:33) at NoRepeats.main(NoRepeats.java:11) "