1

My doubt is that is possible to access the values of the array inside of arraylist? Example:

int[] intArray = new int[];
string[] stringArray = new string[];
stringArray[0] = "Bob";
stringArray[1] = "John";
stringArray[2] = "Alex";
intArray[0] = 5;
intArray[1] = 7;
intArray[2] = 13;
ArrayList listOfArrays = new ArrayList() {intArray , stringArray };

In that example, its possible to access (print) the value of array (inserting the index value) inside of ArrayList?

9
  • 5
    Why are you using ArrayList anyway, it's deprecated Commented Sep 21, 2022 at 13:35
  • I dont know what is good or not, i'm new in this area... i'm just exploring the language. And Charlie i can put more than one datatypes in that arraylist? Commented Sep 21, 2022 at 13:37
  • 5
    Using arrays like this is painful to say the least. Why are you using one array for the names and one for those numbers that seem to match the names? Why not an array of objects with Name and Age properties, or whatever the second array is meant to contain? You can use the record keyword to quickly define eg record Person(string Name,int Age) and then create a List<Person> or Person[] Commented Sep 21, 2022 at 13:37
  • True, but why would you? It sounds like you probably want an array of a class, such as List<Person> Commented Sep 21, 2022 at 13:37
  • put more than one datatypes in that arraylist why do that? That's not natural in any programming language - unless you come from a data science/Python background where Dataframes are constructed from Series/columns instead of rows Commented Sep 21, 2022 at 13:39

3 Answers 3

0

If your exercise is asking to print all the values inside an array with respect to the number of elements inside an array use the for loop with : for(initializer; condition; iterator) in the example below

for(int i = 0; i <intArray.Count; i++){
Console.WriteLine(intArray[i]);
}

But i'd suggest that you clarify more in what you need in your question.

Sign up to request clarification or add additional context in comments.

Comments

0

Your example does not compile. I took the liberty of changing the first 2 lines so that it would.

You can access specific arrays within the ArrayList by index, casting them as the array type that they are. Then, you can access individual elements by index.

The below example gets the string "Alex" from stringArray:

int[] intArray = new int[3];
string[] stringArray = new string[3];
stringArray[0] = "Bob";
stringArray[1] = "John";
stringArray[2] = "Alex";
intArray[0] = 5;
intArray[1] = 7;
intArray[2] = 13;
ArrayList listOfArrays = new ArrayList() { intArray, stringArray };

// Get the string[] by index from the ArrayList:
string[] nestedStringArray = (string[])listOfArrays[1];

// Access an element:
string alex = nestedStringArray[2];

// Or do the above 2 lines in a single line:
alex = ((string[])listOfArrays[1])[2];

If you are just exploring the language, then this should help. However, this is not likely to be a good solution to use in a real application. Storing a variety of types in a single object and then casting in order to access them is a recipe for major headaches.

Comments

-1

listOfArrays[1][2] would access the 3rd element of the stringArray. The first number access the outer array, and the second number access the inner array.

3 Comments

I'm afraid that this does not compile: dotnetfiddle.net/qZD73X
It might need to be tweaked, but that's how you access arrays inside arrays.
This is not an array inside an array. It's an array inside an ArrayList, which behaves differently.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.