import java.util.*;
public class Test {
public static void main(String[] args) {
Set<Integer> set = new TreeSet<Integer>();
List<Integer> list = new ArrayList<Integer>();
for(int i=-3; i<3; i++) {
set.add(i);
list.add(i);
}
System.out.println(set);
System.out.println(list);
for(int i=0; i<3; i++) {
set.remove(i);
list.remove(i);
}
System.out.println(set);
System.out.println(list);
}
}
上面是原程序,我运行后。
结果是:
[-3, -2, -1, 0, 1, 2]
[-3, -2, -1, 0, 1, 2]
[-3, -2, -1]
[-2, 0, 2]
TreeSet和ArrayList有什么区别啊? set中是remove掉了“i"这个对象; 而list中是remove掉了i为索引的那个值这个我清楚,可List和Set这方面区别是啥? 多谢。
public class Test {
public static void main(String[] args) {
Set<Integer> set = new TreeSet<Integer>();
List<Integer> list = new ArrayList<Integer>();
for(int i=-3; i<3; i++) {
set.add(i);
list.add(i);
}
System.out.println(set);
System.out.println(list);
for(int i=0; i<3; i++) {
set.remove(i);
list.remove(i);
}
System.out.println(set);
System.out.println(list);
}
}
上面是原程序,我运行后。
结果是:
[-3, -2, -1, 0, 1, 2]
[-3, -2, -1, 0, 1, 2]
[-3, -2, -1]
[-2, 0, 2]
TreeSet和ArrayList有什么区别啊? set中是remove掉了“i"这个对象; 而list中是remove掉了i为索引的那个值这个我清楚,可List和Set这方面区别是啥? 多谢。
TreeSet public boolean remove(Object o) {
return m.remove(o)==PRESENT;
}ArrayList: public E remove(int index) {
RangeCheck(index); modCount++;
E oldValue = elementData[index]; int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc do its work return oldValue;
}
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
你看看这里是用了哪个方法就明白了。
list remove掉索引0,原来的索引为1的变成索引0,然后再remove掉现在索引1(原来的索引2)
……
http://topic.csdn.net/u/20081022/22/41182840-384f-471b-a8fe-c784bd6c4f82.html
Set 无序不可重复
注:重复可能需要重写equal和hashcode方法
list保存的元素是可以重复的并且是无序的
remove(int index) ,移除此列表中指定位置上的元素。
remove(Object o) 从此列表中移除指定元素的单个实例(如果存在),此操作是可选的。希望我说清楚了!
完全考api
没什么技术含量
public static void main(String[] args) {
Set<Integer> set = new TreeSet<Integer>();
List<Integer> list = new ArrayList<Integer>();
for (int i = -3; i < 3; i++) {
set.add(i);
list.add(i);
System.out.println("i=" + i + "时 set添加为" + set);
System.out.println("i=" + i + "时 list添加为" + list);
}
System.out.println("==================");
System.out.println("set最后结果是 :" + set);
System.out.println("list最后结果是 :" + list);
System.out.println("==================");
for (int i = 0; i < 3; i++) {
set.remove(i);
list.remove(i);
System.out.println("i为 " + i + "时 set中删除的元素为 :" + i);
System.out.println("i为 " + i + "时 list中要删除元素的索引为 :" + i);
}
System.out.println("==================");
System.out.println("set的最后结果是" + set);
System.out.println("list的最后结果是" + list);
System.out.println("==================");
}
}
i=-3时 set添加为[-3]
i=-3时 list添加为[-3]
i=-2时 set添加为[-3, -2]
i=-2时 list添加为[-3, -2]
i=-1时 set添加为[-3, -2, -1]
i=-1时 list添加为[-3, -2, -1]
i=0时 set添加为[-3, -2, -1, 0]
i=0时 list添加为[-3, -2, -1, 0]
i=1时 set添加为[-3, -2, -1, 0, 1]
i=1时 list添加为[-3, -2, -1, 0, 1]
i=2时 set添加为[-3, -2, -1, 0, 1, 2]
i=2时 list添加为[-3, -2, -1, 0, 1, 2]
==================
set最后结果是 :[-3, -2, -1, 0, 1, 2]
list最后结果是 :[-3, -2, -1, 0, 1, 2]
==================
i为 0时 set中删除的元素为 :0
i为 0时 list中要删除元素的索引为 :0 //[-3, -2, -1, 0, 1, 2]索引为0的数是-3 剩下结果为[-2, -1, 0, 1, 2]
i为 1时 set中删除的元素为 :1
i为 1时 list中要删除元素的索引为 :1 //[-2, -1, 0, 1, 2]索引为1的数是-1 剩下结果为[-2, 0, 1, 2]
i为 2时 set中删除的元素为 :2
i为 2时 list中要删除元素的索引为 :2 //[-2, 0, 1, 2]索引为2的数是1 剩下结果为[-2, 0, 2]
==================
set的最后结果是[-3, -2, -1]
list的最后结果是[-2, 0, 2]
==================
今天一看,靠……我TMD还是半桶水!!!!!
谢谢此贴,要是碰上了这题面试,我肯定做错!!! 哦弥陀哦佛!!!!!、
工作,工作工作,工作工作,工作工作,工作工作,工作工作,工作工作,工作工作,工作工作,工作工作,工作工作,工作工作,工作工作,工作工作,工作工作,工作工作,工作工作,工作工作,工作工作,工作工作,工作
ArrayList 内部是用数组实现的
例如我们现在remove掉i=0时的数据
set remove的是值.也就是说,会在结合里找到"0",并将其删除.
list remove的是索引对应的值,就是会将索引为0的那个元素删除.
不过没什么劲啊
list 是根据 index remove
而List是有序的,可以重复的