AL DI
AL DI

Reputation: 560

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

Upvotes: 1

Views: 2302

Answers (4)

junfengshou
junfengshou

Reputation: 1

try this:

    Map<String, List<String>> phoneNumber = new LinkedHashMap<String, List<String>>();
    {
        List<String> phone = new ArrayList<String>();
        phone.add("619-487-5555");
        phone.add("619-487-5556");
        phone.add("619-487-5557");
        phoneNumber.put("Robert", phone);
    }
    {
        List<String> phone = new ArrayList<String>();
        phone.add("123-659-8751");
        phoneNumber.put("Carlos", phone);
    }
    {
        List<String> phone = new ArrayList<String>();
        phone.add("555-555-5555");
        phone.add("6666-555-5555");
        phoneNumber.put("mike", phone);
    }
    for (String person : phoneNumber.keySet()) {
        List<String> phoneList = phoneNumber.get(person);
        for (String phone : phoneList) {
            System.out.println("person: " + person + " | phone: " + phone);
        }
    }

console print:

person: Robert | phone: 619-487-5555
person: Robert | phone: 619-487-5556
person: Robert | phone: 619-487-5557
person: Carlos | phone: 123-659-8751
person: mike | phone: 555-555-5555
person: mike | phone: 6666-555-5555

Upvotes: 0

Nitin Misra
Nitin Misra

Reputation: 76

Just use the following code-:

for(String[] contact: lista) {
       System.out.println("name : "+contact[0]);
       System.out.println("phone : "+contact[1]);
}

You may consider using a HaspMap as other answers suggest.

Upvotes: 0

Scary Wombat
Scary Wombat

Reputation: 44854

Why don't you consider using a Map

e.g

HashMap <String, String> map = new HashMap <> ();

then add

map.put("mike", 911);

get the value

System.out.println ("mikes number is " + map.get("mike"));

Another benefit of using the Map, will be to prevent you having duplicate values (only one "mike")

Upvotes: 0

Philip Rollins
Philip Rollins

Reputation: 1291

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]);
}

Upvotes: 1

Related Questions