1. 程式人生 > 遊戲 >冒險遊戲《拉娜的星球》新實機展示 可在Steam Deck執行

冒險遊戲《拉娜的星球》新實機展示 可在Steam Deck執行

原文地址:(53條訊息) java陣列去重總結_文學超的部落格-CSDN部落格_java陣列去重,怕丟失,純複製

 

1、背景
根據不同的業務邏輯,經常會遇到陣列中存在多個重複元素的場合,總結了下陣列的排序,留個記錄。

2、實現方法

總結了四種方法,接下來進行展示

1、方法一
//陣列去重方法一
String[] array = {"a","b","c","c","d","e","e","e","a"};
List<String> result = new ArrayList<>();
boolean flag;
for(int i=0;i<array.length;i++){
flag = false;
for(int j=0;j<result.size();j++){
if(array[i].equals(result.get(j))){
flag = true;
break;
}
}
if(!flag){
result.add(array[i]);
}
}
String[] arrayResult = (String[]) result.toArray(new String[result.size()]);
System.out.println(Arrays.toString(arrayResult));
先遍歷原陣列,然後遍歷結束集,通過每個陣列的元素和結果集中的元素進行比對,若相同則break。若不相同,則存入結果集。
兩層迴圈進行遍歷得出最終結果。

2、方法二
//陣列去重方法二
String[] array = {"a","b","c","c","d","e","e","e","a"};
List<String> list = new ArrayList<>();
list.add(array[0]);
for(int i=1;i<array.length;i++){
if(list.toString().indexOf(array[i]) == -1){
list.add(array[i]);
}
}
String[] arrayResult = (String[]) list.toArray(new String[list.size()]);
System.out.println(Arrays.toString(arrayResult));
通過使用indexOf方法進行判斷結果集中是否存在了陣列元素。

3、方法三
//陣列去重方法三
String[] array = {"a","b","c","c","d","e","e","e","a"};
List<String> list = new ArrayList<>();
for(int i=0;i<array.length;i++){
for(int j=i+1;j<array.length;j++){
if(array[i] == array[j]){
j = ++i;
}
}
list.add(array[i]);
}
String[] arrayResult = (String[]) list.toArray(new String[list.size()]);
System.out.println(Arrays.toString(arrayResult));
巢狀迴圈,進行比較獲取滿足條件結果集。

4、方法四
//陣列去重方法四
String[] array = {"a","b","c","c","d","e","e","e","a"};
Arrays.sort(array);
List<String> list = new ArrayList<>();
list.add(array[0]);
for(int i=1;i<array.length;i++){
if(!array[i].equals(list.get(list.size()-1))){
list.add(array[i]);
}
}
<pre name="code" class="java"><span style="white-space:pre"> </span>String[] arrayResult = (String[]) list.toArray(new String[list.size()]);
System.out.println(Arrays.toString(arrayResult));
先使用java提供的陣列排序方法進行排序,然後進行一層for迴圈,進行相鄰資料的比較即可獲得最終結果集。
5、方法五
//陣列去重方法五
String[] array = {"a","b","c","c","d","e","e","e","a"};
Set<String> set = new HashSet<>();
for(int i=0;i<array.length;i++){
set.add(array[i]);
}
String[] arrayResult = (String[]) set.toArray(new String[set.size()]);
System.out.println(Arrays.toString(arrayResult));
感謝 漂泊一劍客 的提議,加入set方法進行新增,雖然是無序排列,但是也更方便的解決了去重的問題。

3、知識說明
1、ArrayList集合轉陣列
String[] arrayResult = (String[]) list.toArray(new String[list.size()]);

對應的java方法API

toArray

public Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.

This method acts as bridge between array-based and collection-based APIs.

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>
Overrides:
toArray in class AbstractCollection<E>
Returns:
an array containing all of the elements in this list in proper sequence
See Also:
Arrays.asList(Object[])
toArray

public <T> T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.
If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>
Overrides:
toArray in class AbstractCollection<E>
Parameters:
a - the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
an array containing the elements of the list
Throws:
ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this list
NullPointerException - if the specified array is null

2、陣列直接列印到控制檯

直接呼叫Arrays的toString方法進行轉換再進行列印操作。
例項:
System.out.println(Arrays.toString(arrayResult));
————————————————
版權宣告:本文為CSDN博主「wenxuechaozhe」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處連結及本宣告。
原文連結:https://blog.csdn.net/wenxuechaozhe/article/details/52083936