ArrayList

源码:jdk1.6

1、remove(Object o)操作

public boolean remove(Object o) {
    if (o == null) {
        for (int index = 0; index < size; index++)
        	if (elementData[index] == null) {
        		fastRemove(index);
        		return true;//只删除第一个和o相等的元素
        	}
    	} 
    else {
    	for (int index = 0; index < size; index++)
    		if (o.equals(elementData[index])) {
    			fastRemove(index);
    			return true;//只删除第一个和o相等的元素
    		}
    }
    return false;
}

测试:

import java.util.ArrayList;
import java.util.List;

public class ArrayListStudy {
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("test");
		list.add(null);
		list.add("test");
		list.add(null);
		System.out.println("before remove operation=" + list.toString());
		list.remove("test");
		System.out.println("after remove operation=" + list.toString());
	}
}
结果:
before remove operation=[test, null, test, null]
after remove operation=[null, test, null]

2、remove操作后涉及元素移动操作,故不适于大量删除操作。

 private void fastRemove(int index) {
        modCount++;
        int numMoved = size - index - 1;//需要移动的元素个数
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);//native方法
        elementData[--size] = null; // Let gc do its work
    }
3、遍历涉及remove操作的注意事项

使用iterator的的遍历方式,不能通过原list对象进行remove操作(会产生ConcurrentModificationException异常),只能通过iterator进行remove操作

public class ArrayListStudy {
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("a");
		list.add("b");
		list.add("c");
		list.add("d");
		list.add("e");
		Iterator<String> iter= list.iterator();
		while(iter.hasNext()){
			String str = iter.next();
			if(str.equals("c")){
				iter.remove();
				//list.remove(str);//ConcurrentModificationException
			}
		}
注意:当使用list.remove移除list中倒数第二个元素时,不会出现ConcurrentModificationException(参考:http://hi.baidu.com/yoshubom/item/3f274619baf9a217e3f986b2),并且不能遍历到list的最后一个元素。

public class ArrayListStudy {
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("a");
		list.add("b");
		list.add("c");
		list.add("d");
		list.add("e");
		Iterator<String> iter= list.iterator();
		while(iter.hasNext()){
			String str = iter.next();
			if(str.equals("d")){
				//iter.remove();
				list.remove(str);//ConcurrentModificationException
			}
			System.out.println(str);
		}
		System.out.println(list.toString());
结果:

a
b
c
d
[a, b, c, e]
原因分析:

public boolean hasNext() {
            return cursor != size();
	}
在遍历下一个元素之前都要先执行hasNext方法。当remove倒数个第二个元素后,size()比原来的值少1,故此时hasNext返回true,不执行next()方法,也就没有ConcurrentModificationException异常,同时也不能访问到最后一个元素。

但通过iterator删除时,任然可以正确访问到最后一个元素。

public class ArrayListStudy {
	public static void main(String[] args) {
		
		List<String> list = new ArrayList<String>();
		list.add("a");
		list.add("b");
		list.add("c");
		list.add("d");
		list.add("e");
		Iterator<String> iter= list.iterator();
		while(iter.hasNext()){
			String str = iter.next();
			if(str.equals("d")){
				iter.remove();
				//list.remove(str);//ConcurrentModificationException
			}
			System.out.println(str);
		}
		System.out.println(list.toString());
结果:

a
b
c
d
e
[a, b, c, e]
原因是iterator的remove操作将cursor前移了一位:

public void remove() {
	    if (lastRet == -1)
		throw new IllegalStateException();
            checkForComodification();

	    try {
		AbstractList.this.remove(lastRet);
		if (lastRet < cursor)
		    cursor--;//移除一个元素后,cursor--
		lastRet = -1;
		expectedModCount = modCount;
	    } catch (IndexOutOfBoundsException e) {
		throw new ConcurrentModificationException();
	    }
	}
4、ConcurrentModificationException的设计原理

      见快速失败 fail-fast和故障安全fail-safe: http://www.tuicool.com/articles/umueQf















### 在Unity中使用ArrayList或解决ArrayList相关问题 在Unity开发中,`ArrayList`是非泛型集合类的一部分,允许存储任意类型的元素。尽管C#推荐使用泛型集合(如`List<T>`),但在某些特定场景下,`ArrayList`仍然有其适用性[^4]。 #### 1. 创建和初始化ArrayList 创建一个`ArrayList`对象需要引入`System.Collections`命名空间。以下是一个简单的示例: ```csharp using System.Collections; public class ArrayListExample : MonoBehaviour { void Start() { // 创建一个新的ArrayList实例 ArrayList arrayList = new ArrayList(); // 添加不同类型的元素到ArrayList arrayList.Add("字符串"); arrayList.Add(123); arrayList.Add(45.67f); // 遍历并打印ArrayList中的所有元素 foreach (var item in arrayList) { Debug.Log(item); } } } ``` #### 2. 常见操作:添加、删除和访问元素 `ArrayList`提供了多种方法来操作其中的元素,包括但不限于`Add`、`Remove`、`Insert`和`Contains`等[^4]。 - **添加元素**:使用`Add`方法将新元素追加到集合末尾。 - **插入元素**:使用`Insert`方法在指定索引位置插入新元素。 - **删除元素**:使用`Remove`方法按值删除元素,或使用`RemoveAt`方法按索引删除。 - **查找元素**:使用`Contains`方法检查某个值是否存在于集合中。 以下代码展示了这些操作的具体用法: ```csharp void ExampleOperations() { ArrayList arrayList = new ArrayList { "A", "B", "C" }; // 插入元素 arrayList.Insert(1, "X"); // 删除元素 arrayList.Remove("B"); // 检查元素是否存在 if (arrayList.Contains("X")) { Debug.Log("元素X存在!"); } // 清空整个ArrayList arrayList.Clear(); } ``` #### 3. 解决常见问题 ##### (1) 类型安全问题 由于`ArrayList`是非泛型集合,它会将所有类型视为`Object`处理,这可能导致运行时类型不匹配的问题[^3]。为避免此类问题,建议在访问元素时显式转换类型: ```csharp object element = arrayList[0]; if (element is string str) { Debug.Log(str); } else { Debug.LogError("类型不匹配!"); } ``` ##### (2) 性能问题 当`ArrayList`存储值类型(如`int`或`float`)时,每次添加都会发生装箱操作,而每次访问都会发生拆箱操作,这会导致性能开销[^3]。如果性能是关键因素,推荐使用泛型集合`List<T>`替代`ArrayList`。 #### 4. 替代方案:使用泛型集合`List<T>` 虽然`ArrayList`功能强大,但现代C#开发中更推荐使用泛型集合`List<T>`,因为它提供了更好的类型安全性、更高的性能以及更丰富的API支持[^5]。 以下是一个使用`List<T>`的简单示例: ```csharp using System.Collections.Generic; public class ListExample : MonoBehaviour { void Start() { // 创建一个存储字符串的List List<string> list = new List<string>(); // 添加元素 list.Add("Unity"); list.Add("C#"); // 访问元素 Debug.Log(list[0]); // 遍历列表 foreach (var item in list) { Debug.Log(item); } } } ``` #### 5. 实际应用案例 假设需要动态管理一组游戏对象,可以使用`ArrayList`实现如下功能: ```csharp public class GameObjectManager : MonoBehaviour { private ArrayList gameObjects = new ArrayList(); public void AddGameObject(GameObject obj) { gameObjects.Add(obj); } public void RemoveGameObject(GameObject obj) { gameObjects.Remove(obj); } public void ClearAll() { gameObjects.Clear(); } public void PrintAll() { foreach (var obj in gameObjects) { if (obj is GameObject go) { Debug.Log(go.name); } } } } ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值