重複としてマークする前に質問を読んでください
Util クラスを使用せずに配列から重複を削除する次のコードを作成しましたが、今はスタックしています
public class RemoveDups{
public static void main(String[] args) {
int[] a = { 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 3, 1, 4, 52, 1, 45, };
int temp;
for (int i : a) {
for (int j = 0; j < a.length - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
a = removeDups(a);
for (int i : a) {
System.out.println(i);
}
}
private static int[] removeDups(int[] a) {
int[] result = new int[a.length];
int j = 0;
for (int i : a) {
if (!isExist(result, i)) {
result[j++] = i;
}
}
return result;
}
private static boolean isExist(int[] result, int i) {
for (int j : result) {
if (j == i) {
return true;
}
}
return false;
}
}
そして今、出力は
1
2
3
4
5
6
45
52
0
0
0
0
0
0
0
0
0
0
ここで私の問題は
- 0 の場合、コードが機能しない
- 配列を並べ替えると実行時間が短縮される方法を理解できません
- Util クラスを使用せずに配列から要素を削除する方法はありますか? 配列をリストに変換してから削除する 1 つの方法を知っていますが、そのためにも Util クラスが必要です。自分で実装する方法はありますか。