I am a Java programmer, I have used a Java ArrayList
before and now I want to have something like that in C#. Some of options I need are in this Java code:
我是一个Java程序员,我以前使用过Java ArrayList,现在我想在c#中有类似的东西。我需要的一些选项在这个Java代码中:
String[] strs = new String[]{"str1" , "str2" , "str3" , "str4"};
ArrayList arrayList = new ArrayList(35);
arrayList.add(strs[0]);
arrayList.add(strs[1]);
arrayList.remove(0);
arrayList.set(0, strs[2]);
String s = (String) arrayList.get(1);
I used C# ArrayList
and LinkedList
, but they don't have these simple options that I need. Is there another option in C# supporting accessing objects with indexes, inserting and removing from certain index?
我使用了c# ArrayList和LinkedList,但是它们没有我需要的这些简单选项。c#中是否还有其他选项支持使用索引访问对象、插入和删除某些索引?
2 个解决方案
#1
16
use List <T>
使用< T >列表
String[] strs = new String[]{"str1" , "str2" , "str3" , "str4"};
List<string> stringList = new List<string>();
stringList.add(strs[0]);
stringList.add(strs[1]);
stringList.RemoveAt(indexYouWantToDelete)
String s = stringList[0];
ArrayLists in c# come from the pre-generic era tho. Since C# 2.0 we have generic collections, List <T>
being one example of that. As the comment on this answer says, if you use an ArrayList, the elements that you put into the arraylist will have to be boxed (to Object
, because thats the only thing an ArrayList takes as input). If you want to access them after that, they will have to be explicitly unboxed, like what you did in your question. ( --> String s = (String) arrayList.get(1);
)
c#中的arraylist来自前通用时代tho。因为c# 2.0我们有通用的集合,列表
using generic collections (like List <T>
), there is no boxing anymore, as the compiler knows what datatype the list will consist of. In this case, Strings. You could also have a List<int>
, List<char>
, or List<whatever>
, and you can use the same indexing functionality on them.
使用泛型集合(如List
#2
1
Use List<T>
...........................................
使用< T > ...........................................列表
which has Add
Remove
, RemoveAt
indexers like list[i]
etc.
有添加删除,RemoveAt索引器,如list[i]等。
#1
16
use List <T>
使用< T >列表
String[] strs = new String[]{"str1" , "str2" , "str3" , "str4"};
List<string> stringList = new List<string>();
stringList.add(strs[0]);
stringList.add(strs[1]);
stringList.RemoveAt(indexYouWantToDelete)
String s = stringList[0];
ArrayLists in c# come from the pre-generic era tho. Since C# 2.0 we have generic collections, List <T>
being one example of that. As the comment on this answer says, if you use an ArrayList, the elements that you put into the arraylist will have to be boxed (to Object
, because thats the only thing an ArrayList takes as input). If you want to access them after that, they will have to be explicitly unboxed, like what you did in your question. ( --> String s = (String) arrayList.get(1);
)
c#中的arraylist来自前通用时代tho。因为c# 2.0我们有通用的集合,列表
using generic collections (like List <T>
), there is no boxing anymore, as the compiler knows what datatype the list will consist of. In this case, Strings. You could also have a List<int>
, List<char>
, or List<whatever>
, and you can use the same indexing functionality on them.
使用泛型集合(如List
#2
1
Use List<T>
...........................................
使用< T > ...........................................列表
which has Add
Remove
, RemoveAt
indexers like list[i]
etc.
有添加删除,RemoveAt索引器,如list[i]等。