AL DI AL DI - 3 months ago 7
Java Question

In java how do i print key value array in list?

I am trying to create a simple phone book using java using just an arraylist

import java.util.*;

public class Lista {

public static void menu() {
int opcion = 0;
String[] t2 = new String[2];
ArrayList<String[]> lista = new ArrayList<String[]>();
t2[0] = "Robert";
t2[1] = "619-487-5555";
lista.add(t2);

t2 = new String[2]; // create a new array
t2[0] = "Carlos";
t2[1] = "123-659-8751";
lista.add(t2);

t2 = new String[2];
t2[0] = "mike";
t2[1] = "555-555-5555";
lista.add(t2);
}
}


now how can i access each array in the list to get like

name:mike

phone: 555-555-5555


I am planing to build a menu


/*******************************/

| 1 - Robert |

| 1 - Carlos |

| 1 - Mike |

/********************************/


and when the user inputs lets say numer 2 he will get the corresponding name and phone number on screen.
This is a console program just to demostrate the use of a list

Answer

You can use this in your case.

for (String[] obj: lista)
{
    System.out.print("Name: ");
    System.out.println(obj[0]);

    System.out.print("Number: ");
    System.out.println(obj[1]);
}