大家好,又见面了,我是你们的朋友全栈君。
1、背景
2、实现方法
1、方法一
先遍历原数组,然后遍历结束集,通过每个数组的元素和结果集中的元素进行比对,若相同则break。若不相同,则存入结果集。
2、方法二
通过使用indexOf方法进行判断结果集中是否存在了数组元素。
3、方法三
嵌套循环,进行比较获取满足条件结果集。
4、方法四
System.out.println(Arrays.toString(arrayResult));
先使用java提供的数组排序方法进行排序,然后进行一层for循环,进行相邻数据的比较即可获得最终结果集。
5、方法五
感谢
漂泊一剑客
的提议,加入set方法进行添加,虽然是无序排列,但是也更方便的解决了去重的问题。
3、知识说明
1、ArrayList集合转数组
对应的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>
-
toArray
in interface
List<E>
-
toArray
in class
AbstractCollection<E>
Specified by:
Overrides:
-
-
-
Returns:
- an array containing all of the elements in this list in proper sequence
-
Arrays.asList(Object[])
See Also:
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>
-
toArray
in interface
List<E>
-
toArray
in class
AbstractCollection<E>
Specified by:
Overrides:
-
-
-
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. - an array containing the elements of the list
-
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
Returns:
Throws:
-
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/147821.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...