私はいくつかのプロジェクトオイラーの問題を現在解決しようとしています。なぜArrayListが整数を追加しないのか分かりません。プロジェクトオイラー#4 ArrayListは整数を追加しません
import java.util.ArrayList;
import java.util.Collections;
public class largestPalindromeProduct {
public static void main(String[] args) {
ArrayList<Integer> largestPalindrome = new ArrayList<Integer>();
for (int x = 999; x >= 100; x--) {
for (int y = 999; y >= 100; y--) {
int result = x * y;
if(isPalindrome(result)) {
largestPalindrome.add(result);
break;
// System.out.println("Added Palindrome: " +result);
}
}
}
System.out.println(Collections.max(largestPalindrome));
}
public static boolean isPalindrome(int n) {
String newN = Integer.toString(n);
// System.out.println("newN =" +newN);
StringBuilder sBuilder = new StringBuilder(newN);
// System.out.println("sBuilder reverse = " +sBuilder.reverse());
if (newN.equals(sBuilder.reverse())) {
return true;
} else return false;
}
}
これは例外です。あなたがタイプStringBuilder
とは、ArrayListに追加されますString#equals
ので、何もタイプString
を比較しているので、
Exception in thread "main" java.util.NoSuchElementException
at java.util.ArrayList$Itr.next(ArrayList.java:854)
at java.util.Collections.max(Collections.java:669)
at largestPalindromeProduct.main(largestPalindromeProduct.java:18)
おかげで、私の質問へのあなたの親切な返事を。私の問題を解決しました。 通常、私はEclipseを使用していますが、現在はVisual Studio Codeに慣れようとしています。 – jeremypuchta