我正在嘗試查找字符串的所有可能的不同子字符串。這是我當前的代碼:給定字符串的不同子字符串
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
StringBuffer f = new StringBuffer();
//Get a string of possible substrings with out uniqueness
for (int i = 0; i < str.length(); i++) {
for (int j = i; j < str.length(); j++) {
f.append(str.charAt(j) + ",");
}
}
f.deleteCharAt(sb.length() - 1);
String s = f.toString();
String[] arr = s.Split(',');
arr = arr.Distinct().ToArray();
for (int i = 0; i < arr.length; i++)
System.out.println(arr[i]);
}
}
編譯時收到多個錯誤。我不明白代碼出錯的地方。我忘了導入課程還是有語法錯誤?
Solution.java:28: error: cannot find symbol
f.deleteCharAt(sb.length()-1);
^
symbol: variable sb
location: class Solution
Solution.java:31: error: cannot find symbol
String[] arr = s.Split(',');
^
symbol: method Split(char)
location: variable s of type String
Solution.java:34: error: cannot find symbol
arr = arr.Distinct().ToArray();
^
symbol: method Distinct()
location: variable arr of type String[]
3 errors
'final'是Java中的保留字。嘗試調用其他變量。 – legoscia
另外Java是區分大小寫的,所以'Length'!='length'。另一件事是'String'沒有'length'字段,但是它有'length()'方法。 – Pshemo
我會補充說,Split()是split()(大寫) – Tavo