How to initialize a list of strings (Liststring) with many string values
List<string> mylist = new List<string>(new string[] { "element1", "element2", "element3" });
How to initialize ListString object in Java?
If you check the API for List
you'll notice it says:
Interface List<E>
Being an interface
means it cannot be instantiated (no new List()
is possible).
If you check that link, you'll find some class
es that implement List
:
All Known Implementing Classes:
AbstractList
,AbstractSequentialList
,ArrayList
,AttributeList
,CopyOnWriteArrayList
,LinkedList
,RoleList
,RoleUnresolvedList
,Stack
,Vector
Some of those can be instantiated (the ones that are not defined as abstract class
). Use their links to know more about them, I.E: to know which fits better your needs.
The 3 most commonly used ones probably are:
List<String> supplierNames1 = new ArrayList<String>();
List<String> supplierNames2 = new LinkedList<String>();
List<String> supplierNames3 = new Vector<String>();
Bonus:
You can also instantiate it with values, in an easier way, using the Arrays
class
, as follows:
List<String> supplierNames = Arrays.asList("sup1", "sup2", "sup3");
System.out.println(supplierNames.get(1));
But note you are not allowed to add more elements to that list, as it's fixed-size
.
How can I initialize a C# List in the same line I declare it. (IEnumerable string Collection Example)
var list = new List<string> { "One", "Two", "Three" };
Essentially the syntax is:
new List<Type> { Instance1, Instance2, Instance3 };
Which is translated by the compiler as
List<string> list = new List<string>();
list.Add("One");
list.Add("Two");
list.Add("Three");
How to make a new List in Java
List myList = new ArrayList();
or with generics (Java 7 or later)
List<MyType> myList = new ArrayList<>();
or with generics (Old java versions)
List<MyType> myList = new ArrayList<MyType>();
How to Create and Initialize (in Java) a ListString[] in the same statement? (Editors please NOTE: this *not* the same as just a ListString)
The correct answer was given in the comments (above) by @user16320675 , and this works well. It handles both the creation of the outer List and the internal array of strings. Other answers pointing to a simple Array asList are for simple strings, which is not the question that was asked.
to create a mutable list
final List<String[]> rolesCsv =
new ArrayList<String[]>Collections.singletonList(new String[]{"Role Name","Description"}));
Otherwise (despite the array, its element, will not be immutable)
final List<String[]> rolesCsv =
Collections.singletonList(new String[]{"Role Name","Description"})
Initialize list of strings with 2 strings
You would just return a variable of type List. IE:
public async static Task<List<String>> TagMonatJahr()
{
string var1= String.Empty;
string var2 = String.Empty;
return new List<String>{var1, var2};
}
why we cannot add values directly in Liststring obj= new Liststring()?
You can't write code like that in a class body
You have 2 options, either set the contents of your list when you declare it
List<string> cities = new List<string>() {"pune", "Mumbai"};
Alternatively you can do what you're currently doing, but move some of your code into the class' constructor
public class ValuesController : ApiController
{
List<string> cities = new List<string>();
public ValuesController()
{
cities.Add("pune");
cities.Add("Mumbai");
}
// GET api/values
public IEnumerable<string> Get()
{
return cities ;
}
}
Read more about class constructors here
You might also want to consider making cities
a private field
C# how to initialize list with values from different list using lambdas or something else -- need to simplify
Looks like it might be something like
Clinician = new Clinician()
{
Roles = expectedRolesString.Split(',',';').Select(r => Enum.Parse<RoleModel>(r.Trim())).ToList()
}
or perhaps
Clinician = new Clinician()
{
Roles = expectedRolesString.Split(new[]{',',';',' '}, StringSplitOptions.RemoveEmptyEntries).Select(Enum.Parse<RoleModel>).ToList()
}
..but I'd have liked to see the input data and enum definition to make sure
Adding a string List to a dictionary, then retrieving it's values
I assume the scenarioContext
is declared as Dictionary<string, List<string>>
, you can retrieve the value, which is a list in this case, by key. This is how your code would look like:
Option 1
Dictionary<string, List<string>> scenarioContext = new Dictionary<string, List<string>>();
List<string> myList = new List<string>();
myList.Add("Thomas");
myList.Add("Bob");
myList.Add("Andy");
scenarioContext.Add("myList", myList);
List<string> newList = new List<string>();
newList = scenarioContext["myList"];
Update: I just found out that ScenarioContext in SpecFlow has an additional Getter. You are casting the List as string in your last line of code, it should look like this:
NewList = this.ScenarioContext.Get<List<string>>("myList");
Related Topics
Capturing Binary Output from Process.Standardoutput
Get SQL Query from Linq to SQL
Shellexecute Equivalent in .Net
Illustrating Usage of the Volatile Keyword in C#
Send Smtp Email Using System.Net.Mail via Exchange Online (Office 365)
Create Xml Nodes Based on Xpath
How to Delete a File Which Is Locked by Another Process in C#
High Quality Jpeg Compression with C#
Streamwriter and Utf-8 Byte Order Marks
What Is the JSON.Net Equivalent of Xml's Xpath, Selectnodes, Selectsinglenode
Open Default Mail Client Along with a Attachment
Equivalent of SQL Isnull in Linq
Difference Between "Var" and "Object" in C#
What's the Difference Between Iequatable and Just Overriding Object.Equals()
How to Get the Time Difference Between Two Datetime Objects Using C#
How to Specify My Explicit Type Comparator Inline
Fast Creation of Objects Instead of Activator.Createinstance(Type)