How to check if element already exist in an array [duplicate] - c#

This question already has answers here:
Method to check array list containing specific string
(4 answers)
Closed 3 years ago.
I got an ArrayList in C#
ArrayList myAL = new ArrayList();
now I add a string to it
myAL.Add("Hello");
How can I now figure out if the String "Hello" exist in the array. I know it's possible to loop over it, but I think there must exist a function or something.

You can simply use the method Contains from System.Collections
Microsoft Doc - Contains
For your example :
ArrayList myAL = new ArrayList();
myAL.Add("Hello");
if (myAL.Contains("Hello"))
{
// Do something
}

Related

Adding objects from a constructor to an ArrayList [duplicate]

This question already has answers here:
WriteLine with a class
(9 answers)
Print out object elements from list [duplicate]
(2 answers)
Print List of objects to Console
(3 answers)
Class List Keeps Printing Out As Class Name In Console?
(4 answers)
Why / when would it be appropriate to override ToString?
(17 answers)
Closed 2 years ago.
I'm trying to create an ArrayList with cars I've created using a constructor class and then afterwards use a foreach loops to then print the model name of each cars.
I've created the car objects as following
Car car1 = new Car("Audi e-tron", "Black", 2021);
Car car2 = new Car("Audi RS7", "Black", 2021);
Car car3 = new Car("Audi A5", "Black", 2021);
I created the ArrayList and attempted to begin adding the car objects to the ArrayList
ArrayList carList = new ArrayList();
carList.Add(car1);
carList.Add(car2);
carList.Add(car3);
I tested to see if the code was working properly up to this point and checked if the ArrayList has populated
Console.WriteLine("car list: {0}", carList[0]);
but that returns
ArrayListPractice.MainClass+Car
How can I add objects from the constructor to the ArrayList and return a parameter of the object? Thank you!
What exactly are you trying to print in the code block below?
Console.WriteLine("car list: {0}", carList[0]);
If you want to print the string property you passed into the constructor of the first element ("Audi e-tron"), for example, try this:
Console.WriteLine("car list: {0}", carList[0].Name); // 'Name' should be the field name you're looking for
You should override the Car object, ToString() method and there is no need to other considerations, it will be automatically done

how to instanciate a list with an array of list like in the example? [duplicate]

This question already has answers here:
Initializing collections that contain nested collections
(3 answers)
Closed 6 years ago.
I have to make two lists in C # . How do the second list ?
The first is well and works , the second already tried as exemplified below but does not work . And I want it to be exactly like or similar without the use of other variables
List<String> t = new List<string>() {"question","anotherQuestion"};
then i want another list
and i would like to initialize it like this.
List<String[]> t = new List<string[]>()
{
("rightawnser","wrongAwnser"), //1st question
("rightawnser","wrongAwnser"), //2nd question
};
Close, but you have to create an array for each list entry:
List<string[]> t = new List<string[]>()
{
new[] { "rightawnser","wrongAwnser" }, //1st question
new[] { "rightawnser","wrongAwnser" }, //2nd question
};

c# adding a string to an array [duplicate]

This question already has answers here:
How to add a string to a string[] array? There's no .Add function
(15 answers)
C# declare empty string array
(10 answers)
Closed 7 years ago.
i want to simply add a string to an array, like this:
string[] arrayName = new string[0];
arrayName.Add("raptor");
But this doesn't work, can someone help me?
You should use a generic List(Of T).
List<string> myStrings = new List<string>();
myStrings.Add("raptor");
and if you really want an array:
string[] myStringArray = myStrings.ToArray();

How to get part of a string[] array in C# [duplicate]

This question already has answers here:
How do I clone a range of array elements to a new array?
(26 answers)
Closed 7 years ago.
How, using C#, can I create a new string[] from another string[] containing a subsection of the old string[] array?
For example, if I had:
string[] a = { "cat", "dog", "hamster", "parrot" }
And I wanted to get everything after the first element I should get:
string[] b = { "dog", "hamster", "parrot" }
How, if this is even possible without a for loop, do I achieve this?
You can use some extensions methods to solve it, that is the case for Skip(int) under the System.Linq namespace, for sample:
string[] b = a.Skip(1).ToArray();
And it will skip 1 element and convert the result into a new array. Remember to add the namespace:
using System.Linq;
As the MSDN documentation said:
Bypasses a specified number of elements in a sequence and then returns
the remaining elements.

Object refrence not set to an instance of an object in array [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 8 years ago.
i am very new to C# programming so my question may sound very silly
actualy i am creating an multidimensional string array like
public class master
{
public List<string> user_selected = new List<string>();
public List<string> available = new List<string>();
public List<string> bookedseats = new List<string>();
public string [] [] trackbooked=new string[30][] ;
}
now i am assigning some values to it like
a[l].trackbooked[i][j] = pb.Name;
a is a list of object
List<master> a = new List<master>();
a.Add(obj0);
a.Add(obj1);
a.Add(obj2);
a.Add(obj3);
a.Add(obj4);
can some one plz help.thank u in adv.
You've only initialized one dimension of your multidimensional array. See msdn for all the ways you can initialize a multidimensional array.
public string [,] trackbooked=new string[30,30] ;
pb.Name.Tostring();
I would venture to guess that whatever pb is that its NAME value isn't returned as a string. You should be able to use the above command to correct your issue.

Categories

Resources