您的位置:首页 > 其它

集合,数组相互转换,以及相关操作

2016-03-24 10:34 459 查看
1.把SET变成List:

new ArrayList( ).add( Set<>);

2.把数组变成List:

Arrays.asList( 数组 )

这种情况,生成的list,无法添加数据等操作,所以应该写成这样:

List<String> list = new ArrayList<String>(Arrays.asList(new String[]{"a","b"}));

3.集合变成数组:

Collection.toArray();

4.后台输出基本类型的list

Arrays.toString(list.toArray());

-----------------------------------------------------------------------------------------------------

5.操作

类 Arrays

此类包含用来操作数组(比如排序和搜索)的各种方法。此类还包含一个允许将数组作为列表来查看的静态工厂。如果指定数组引用为 null,则此类中的方法都会抛出 NullPointerException。

public static <T> List<T> asList(T... a)

此方法同 Collection.toArray() 一起,充当了基于数组的 API 与基于 collection 的 API 之间的桥梁。返回的列表是可序列化的,并且实现了 RandomAccess。

public static void sort(Object[] a)

根据元素的自然顺序对指定对象数组按升序进行排序。数组中的所有元素都必须实现 Comparable 接口。此外,数组中的所有元素都必须是可相互比较的(也就是说,对于数组中的任何 e1 和 e2 元素而言,e1.compareTo(e2) 不得抛出 ClassCastException)。

public static <T> void sort(T[] a, Comparator<? super T> c)

public static void sort(byte[] a,int fromIndex,int toIndex)

对指定 byte 型数组的指定范围按数字升序进行排序。排序的范围从索引 fromIndex(包括)一直到索引 toIndex(不包括)。(如果 fromIndex==toIndex,则排序范围为空。)

public class ArraysTest {

public static void main(String[] args) {

String[] strArrays = new String[]{"35","3","4356","a","C","A"};

Arrays.sort(strArrays);

for(String s: strArrays) {

System.out.println(s);

}

}

}

输出:

3

35

4356

A

C

a

public static void test02() {

String[] strArrays = new String[]{"35","C","4356","3","2","A"};

Arrays.sort(strArrays,0,3);

for(String s: strArrays) {

System.out.println(s);

}

}

35

4356

C

3

2

A

List

所有超级接口:

Collection<E>, Iterable<E>

可以插入多个Null元素

方法:

boolean addAll(Collection<? extends E> c)

添加指定 collection 中的所有元素到此列表的结尾,顺序是指定 collection 的迭代器返回这些元素的顺序(可选操作)。

boolean contains(Object o)

如果列表包含指定的元素,则返回 true。更确切地讲,当且仅当列表包含满足 (o==null ? e==null : o.equals(e)) 的元素 e 时才返回 true。

boolean addAll(int index,Collection<? extends E> c)

将指定 collection 中的所有元素都插入到列表中的指定位置(可选操作)。

boolean removeAll(Collection<?> c)

从列表中移除指定 collection 中包含的其所有元素(可选操作)。

boolean retainAll(Collection<?> c)

仅在列表中保留指定 collection 中所包含的元素(可选操作)。换句话说,该方法从列表中移除未包含在指定 collection 中的所有元素。

void clear()

从列表中移除所有元素(可选操作)。此调用返回后该列表将是空的。

boolean equals(Object o)

比较指定的对象与列表是否相等。当且仅当指定的对象也是一个列表、两个列表有相同的大小,并且两个列表中的所有相应的元素对相等 时才返回 true( 如果 (e1==null ? e2==null :e1.equals(e2)),则两个元素 e1 和 e2 是相等 的)。换句话说,如果所定义的两个列表以相同的顺序包含相同的元素,那么它们是相等的

E get(int index)

返回列表中指定位置的元素。

E set(int index,E element)用指定元素替换列表中指定位置的元素(可选操作)。

void add(int index,

E element)在列表的指定位置插入指定元素(可选操作)。

E remove(int index)

移除列表中指定位置的元素(可选操作)。将所有的后续元素向左移动(将其索引减 1)。返回从列表中移除的元素。

int indexOf(Object o)

返回此列表中第一次出现的指定元素的索引;如果此列表不包含该元素,则返回 -1。更确切地讲,返回满足 (o==null ? get(i)==null : o.equals(get(i))) 的最低索引 i;如果没有这样的索引,则返回 -1。

List<E> subList(int fromIndex,

int toIndex)

返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之间的部分视图。

public static void test03() {

List<String> strList = new ArrayList<String>();

strList.add("e");

strList.add("m");

strList.add("p");

strList.add("o");

Set<String> set = new HashSet<String>();

set.add("p");

Set<String> setRemove = new HashSet<String>();

setRemove.add("e");

setRemove.add("f");

if(strList.removeAll(setRemove))//从列表中移除指定 collection 中包含的其所有元素

System.out.println(strList);

boolean flag = strList.retainAll(set);//从列表中移除未包含在指定 collection 中的所有元素

if(flag) {

System.out.println(strList);

}

}

[m, p, o]

[p]

public static void test04() {

List<String> strList = Arrays.asList(new String[]{"a","e","o","w","r","c","we","l"});

List<String> temp = strList.subList(0, 4);//0------3

System.out.println(temp);

List<String> temp2 = strList.subList(3, 6);//3----5

System.out.println(temp2);

temp2.set(0, "8");//用指定元素替换列表中指定位置的元素

System.out.println(temp2);

}

[a, e, o, w]

[w, r, c]

[8, r, c]

//remove操作只能用于具体类型,List是接口,不能直接用remove,一次remove不能删除重复数据,只能删除一次,removeAll()可以删除重复数据。

public static void test05(){

List<String> strList = Arrays.asList(new String[]{"a","e","o","w","r","c","we","l","c","c"});

List<String> temp = new ArrayList<String>();

temp.clear();

temp.addAll(strList);

String s = temp.get(3);

System.out.println(s + "-------------------");

temp.remove(s);

System.out.println(temp);

System.out.println("========================================");

temp.remove("c");

System.out.println(temp);

Set<String> set = new HashSet<String>();

set.add("c");

temp.removeAll(set);

System.out.println(temp);

}

w-------------------

[a, e, o, r, c, we, l, c, c]

========================================

[a, e, o, r, we, l, c, c]

[a, e, o, r, we, l]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: