C # arrayList of Objects

0

I have an arrayList called aL that contains vectors.

                        ArrayList aL new ArrayList();

                        String[,] datos = new String[1,2];
                        elementos[0,0] = "Juan";
                        elementos[0,1] = "1258";
                        aL.add(datos);

                        String[,] datos = new String[1,2];
                        elementos[0,0] = "Pedro";
                        elementos[0,1] = "2684";
                        aL.add(datos);

The arrayList will be filled with vectors. The problem is that I do not know how to access the elements of the vectors that I am adding.

can you help me?

    
asked by Abner 06.09.2017 в 06:59
source

3 answers

1

When adding the "data" objects to the ArrayList, to be able to consult them later, you have to access the ArrayList.

According to your code, in the first position of aL, you have one data object and in the second one another.

To get the first aL object, you have to do the following:

String[,] dato1 = aL[0]; //Como si fuese un array

Once you have obtained the element, to access its properties it is very simple.

string nombre = dato1[0][0];  //Esto debería devolver Juan

If you would like to obtain all the data of your list you can do it like this:

for(int i=0; i<aL.Count; i++)
{
    string[,] dato = aL[i]; //Cada iteración del bucle tendremos un elemento del listado
}
    
answered by 06.09.2017 / 08:33
source
1

I can not stand the curiosity to see how to solve this in the terms and this is the result:.

  ArrayList aL = new ArrayList();
        String[,] datos = new String[1, 2];
        datos[0, 0] = "Juan";
        datos[0, 1] = "1258";
        aL.Insert(0, datos); // Inserta el Array en la posicion 0 del ArrayList

        String[,] datos2 = new String[1, 2];
        datos2[0, 0] = "Pedro";
        datos2[0, 1] = "2684";
        aL.Insert(1,datos2); // Inserta el Array en la posicion 1 del ArrayList

        for (int i = 0; i < aL.Count; i++)
        {
            string[,] ArrayEnArrayList = (System.String[,])aL[i];
            System.Console.WriteLine("Elemento del ArrayList: " + i.ToString());

            int numeroDeFilas = ArrayEnArrayList.GetLength(0);
            int numeroDeColumnas = ArrayEnArrayList.GetLength(1);

            for (int f = 0; f < numeroDeFilas; ++f)
            {
                for (int c = 0; c < numeroDeColumnas; ++c)
                {
                    Console.WriteLine(ArrayEnArrayList[f,c]);
                }
            }
        }
        System.Console.ReadLine();
    
answered by 06.09.2017 в 09:55
-1

The first thing you are using Matrices, as we do not know how to use it, I recommend you better Vectors. Also better to use List. There goes:

List<string[]> lista = new List<string[]>();
lista.Add(new string[] { "Juan", "1258" });
lista.Add(new string[] { "Pedro", "2684" });

//Si quieres recuperar una posición en concreto..
string[] juan = lista[0];
//Si prefieres recuperar por nombre.. por ejemplo Pedro
string[] pedro = lista.Find(find => find[0].Equals("Pedro"));
    
answered by 08.09.2017 в 14:22