c#等价于java arraylist支持get、set和remove某些索引

时间:2022-09-03 07:17:32

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我们有通用的集合,列表 就是一个例子。正如对这个答案的评论所说,如果你使用ArrayList,你放入ArrayList的元素必须被装箱(对象,因为这是ArrayList作为输入的唯一的东西)。如果你想要在那之后访问他们,他们必须明确地取消限制,就像你在你的问题中所做的那样。(——>字符串s = (String) arrayList.get(1);)

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 ),不再存在装箱,因为编译器知道列表将包含哪些数据类型。在本例中,字符串。您还可以有一个列表 , List ,或List< any >,您可以在它们上使用相同的功能。

#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我们有通用的集合,列表 就是一个例子。正如对这个答案的评论所说,如果你使用ArrayList,你放入ArrayList的元素必须被装箱(对象,因为这是ArrayList作为输入的唯一的东西)。如果你想要在那之后访问他们,他们必须明确地取消限制,就像你在你的问题中所做的那样。(——>字符串s = (String) arrayList.get(1);)

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 ),不再存在装箱,因为编译器知道列表将包含哪些数据类型。在本例中,字符串。您还可以有一个列表 , List ,或List< any >,您可以在它们上使用相同的功能。

#2


1  

Use List<T> ...........................................

使用< T > ...........................................列表

which has Add Remove, RemoveAt indexers like list[i] etc.

有添加删除,RemoveAt索引器,如list[i]等。