Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing ArrayList with constant literal

Can the ArrayList below be initialized directly without the need for aFileExt string array?

private static string[] aFileExt =       {"css", "gif", "htm", "html", "txt", "xml" }; private System.Collections.ArrayList alFileTypes =      new System.Collections.ArrayList(aFileExt); 

The line below is the goal, but my .Net Compiler does not like it:

private static System.Collections.ArrayList alFileTypes =       new System.Collections.ArrayList({"css","gif","htm","html","txt","xml"}); 

I am using the .net Micro Framework and thus do not have access to generic types.

like image 403
MandoMando Avatar asked Nov 12 '09 15:11

MandoMando


People also ask

Can you initialize ArrayList with values?

Java developers use the Arrays. asList() method to initialize an ArrayList. Using asList() allows you to populate an array with a list of default values. This can be more efficient than using multiple add() statements to add a set of default values to an ArrayList.

Can you initialize an ArrayList?

To initialize an ArrayList in Java, you can create a new ArrayList with new keyword and ArrayList constructor. You may optionally pass a collection of elements, to ArrayList constructor, to add the elements to this ArrayList. Or you may use add() method to add elements to the ArrayList.

How do you create and initialize an ArrayList in one line?

Actually, probably the "best" way to initialize the ArrayList is the method you wrote, as it does not need to create a new List in any way: ArrayList<String> list = new ArrayList<String>(); list. add("A"); list. add("B"); list.


2 Answers

C# 1 or 2:

private static ArrayList alFileTypes =       new ArrayList(new string[] {"css","gif","htm","html","txt","xml"}); 

C# 3 using an implicitly typed array:

private static ArrayList alFileTypes =      new ArrayList(new[] {"css","gif","htm","html","txt","xml"}); 

C# 3 using a collection initializer:

private static ArrayList alFileTypes =      new ArrayList{"css","gif","htm","html","txt","xml"}; 

Or create your own helper method:

public static ArrayList CreateList(params object[] items) {     return new ArrayList(items); } 

then:

static ArrayList alFileTypes = CreateList("css","gif","htm","html","txt","xml"); 

Any reason why you're not using the generic collections, btw?

like image 146
Jon Skeet Avatar answered Oct 04 '22 15:10

Jon Skeet


If you're using .NET 2.0 or greater, you should be using the generic List<T> type (even if it's List<object>, which would given you the same functionality as ArrayList).

If you're using .NET 3.5 or greater, you can use this syntax:

private static List<string> fileTypes = new List<string>() {      "css","gif","htm","html","txt","xml"  }; 

Either way, however, if you want to stick with ArrayList, you can just do:

private static System.Collections.ArrayList alFileTypes =   new System.Collections.ArrayList(new object[] {"css","gif","htm","html","txt","xml"}); 
like image 35
Adam Robinson Avatar answered Oct 04 '22 14:10

Adam Robinson