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?

elmer007
  • 1,412
  • 14
  • 27
Mikayo
  • 11
  • 1
  • 5
    Why are you using `ArrayList` anyway, it's deprecated – Charlieface Sep 21 '22 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? – Mikayo Sep 21 '22 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` or `Person[]` – Panagiotis Kanavos Sep 21 '22 at 13:37
  • True, but why would you? It sounds like you probably want an array of a class, such as `List` – Charlieface Sep 21 '22 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 – Panagiotis Kanavos Sep 21 '22 at 13:39
  • Hm... okay, now its clear, sorry about that. i'll try that way u posted. My intention it's to make an "DB" inside of the program (for practice). But Thanks for Charlie and Panagiotis ;) Good day ! – Mikayo Sep 21 '22 at 13:44
  • "_i can put more than one datatypes in that arraylist_" Sure, and you will then have lots of fun consuming such an array, with all the if's and switch-cases and pattern matching for all the instances of different types you have in your array. I mean, i am not against fun, so have at it. You got my blessing... –  Sep 21 '22 at 13:53
  • @Mikayo If you are just exploring the language, my answer below shows how to access individual elements. However, as previous comments have noted, this is not likely to be a good solution in a real application – elmer007 Sep 21 '22 at 13:55
  • The linked duplicate is not a duplicate. This question is about `ArrayList`, while the other question is about jagged arrays – elmer007 Sep 21 '22 at 13:59

3 Answers3

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.

elmer007
  • 1,412
  • 14
  • 27
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.

Brainmated
  • 17
  • 1
  • 6
-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.

Joe
  • 212
  • 2
  • 11