What is ArrayList.FixedSize(Argument arg)

从这里我们开始讨论的话题,牵涉到ArrayList.FixedSize用法的问题, 测试 添加/删除 是否可用, 而赋值操作是否可行

从一个测试程序开始, 思路简单,只是简单的操作,如果出现异常则输出错误消息

1:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Linq.Expressions;

namespace JustForTesting
{
class Program
{


static void Main(string[] args)
{


IList targetList
= null;
List
<int> list = new List<int>() { 1, 2, 3, 4 };

targetList
= ArrayList.FixedSize(list);
// Try to assign a value
try
{
targetList[
0] = 5;
}
catch (Exception)
{
Console.WriteLine(
"Assign a value to List failed");
}

// Try to remove a value
try
{
targetList.RemoveAt(
3);
}
catch (Exception)
{
Console.WriteLine(
"Remove a value from List failed");
}
// Try to add a value \
try
{
targetList.Add(
6);
}
catch (Exception)
{
Console.WriteLine(
"Add a value to list failed");

}
foreach (int i in targetList)
{
Console.Write(
"{0}\t ", i);
}
Console.WriteLine();
Console.WriteLine(
"Origion List");
foreach (int i in list)
{
Console.Write(
"{0}\t ", i);
}
Console.WriteLine();

// --------------------------------
// For the ArrayList
ArrayList alist = new ArrayList() { 1, 2, 3, 4 };
ArrayList target
= ArrayList.FixedSize(alist);
try
{
target[
0] = 5;
}
catch (Exception)
{
Console.WriteLine(
"Assign a value to ArrayList failed");
}


// Try to remove a value
try
{
target.RemoveAt(
3);
}
catch (Exception)
{
Console.WriteLine(
"Remove a value from ArrayList failed");
}
// Try to add a value
try
{
target.Add(
6);
}
catch (Exception)
{
Console.WriteLine(
"Add a value to ArrayList failed");
}

foreach (int i in target)
{
Console.Write(
"{0}\t ", i);
}
Console.WriteLine();
Console.WriteLine(
"Origion List");
foreach (int i in list)
{
Console.Write(
"{0}\t ", i);
}

Console.WriteLine();
Console.Read();

}
}
}

 


输出结果

ArrayFixedSize的算法如何呢?

其实只是给他添加了一个引用,用另外一个类给原来的List做了一个包装,在访问对应方法的时候抛出异常。这里我想和装饰者模式一起联想下会OK

装饰类的代码如下

 

 [Serializable()] private class FixedSizeArrayList : ArrayList
{
private ArrayList _list;

internal FixedSizeArrayList(ArrayList l) {
_list
= l;
_version
= _list._version;
}

public override int Count {
get { return _list.Count; }
}

public override bool IsReadOnly {
get { return _list.IsReadOnly; }
}

public override bool IsFixedSize {
get { return true; }
}

public override bool IsSynchronized {
get { return _list.IsSynchronized; }
}

public override Object this[int index] {
get {
return _list[index];
}
set {
_list[index]
= value;
_version
= _list._version;
}
}

public override Object SyncRoot {
get { return _list.SyncRoot; }
}

public override int Add(Object obj) {
throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection"));
}

public override void AddRange(ICollection c) {
throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection"));
}

public override int BinarySearch(int index, int count, Object value, IComparer comparer) {
return _list.BinarySearch(index, count, value, comparer);
}

public override int Capacity {
get { return _list.Capacity; }
set { throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); }
}

public override void Clear() {
throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection"));
}

public override Object Clone() {
FixedSizeArrayList arrayList
= new FixedSizeArrayList(_list);
arrayList._list
= (ArrayList)_list.Clone();
return arrayList;
}

public override bool Contains(Object obj) {
return _list.Contains(obj);
}

public override void CopyTo(Array array, int index) {
_list.CopyTo(array, index);
}

public override void CopyTo(int index, Array array, int arrayIndex, int count) {
_list.CopyTo(index, array, arrayIndex, count);
}

public override IEnumerator GetEnumerator() {
return _list.GetEnumerator();
}

public override IEnumerator GetEnumerator(int index, int count) {
return _list.GetEnumerator(index, count);
}

public override int IndexOf(Object value) {
return _list.IndexOf(value);
}

public override int IndexOf(Object value, int startIndex) {
return _list.IndexOf(value, startIndex);
}

public override int IndexOf(Object value, int startIndex, int count) {
return _list.IndexOf(value, startIndex, count);
}

public override void Insert(int index, Object obj) {
throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection"));
}

public override void InsertRange(int index, ICollection c) {
throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection"));
}

public override int LastIndexOf(Object value) {
return _list.LastIndexOf(value);
}

public override int LastIndexOf(Object value, int startIndex) {
return _list.LastIndexOf(value, startIndex);
}

public override int LastIndexOf(Object value, int startIndex, int count) {
return _list.LastIndexOf(value, startIndex, count);
}

public override void Remove(Object value) {
throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection"));
}

public override void RemoveAt(int index) {
throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection"));
}

public override void RemoveRange(int index, int count) {
throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection"));
}

public override void SetRange(int index, ICollection c) {
_list.SetRange(index, c);
_version
= _list._version;
}

public override ArrayList GetRange(int index, int count) {
if (index < 0 || count < 0)
throw new ArgumentOutOfRangeException((index<0 ? "index" : "count"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (Count - index < count)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
return new Range(this,index, count);
}

public override void Reverse(int index, int count) {
_list.Reverse(index, count);
_version
= _list._version;
}

public override void Sort(int index, int count, IComparer comparer) {
_list.Sort(index, count, comparer);
_version
= _list._version;
}

public override Object[] ToArray() {
return _list.ToArray();
}

public override Array ToArray(Type type) {
return _list.ToArray(type);
}

public override void TrimToSize() {
throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection"));
}
}

posted on 2008-11-29 09:36  xwang  阅读(653)  评论(0编辑  收藏  举报

导航