Java multi-dimension array -


i have 2 dimension array of username , surname , able print both name , surname.but want below , looking help.

1) match name string , output name , surname both

tried if loop did not fails print surname.

if string name give abc should match array , print both values of name , surname.

string[][] names = {{"abc","def"},{"ghi","jkl"}};        string name = "abc";         (int = 0; < names.length; i++) {             system.out.print(names[i][0] + ": ");            (int j = 1; j < names[i].length; j++) {                system.out.print(names[i][j] + " ");            }            system.out.println();            } 

try this:

for (string[] fullname : names) {     if (fullname[0].equals(name)) {         (string s : fullname) {             system.out.print(s + " ");         }         system.out.println();     } } 

if not know these for-loops: iterate on each element in order, i.e. without having use index accessing elements. these same:

for (int = 0; < array.length; i++) {     string element = array[i]; }  (string element : array) {  } 

another possibility use hashmap:

string name = "abc";  hashmap<string, string> fullnames = new hashmap<>(); fullnames.put("abc", "def"); fullnames.put("ghi", "jkl");  if (fullnames.contains(name)) {     system.out.println(name + " " + fullnames.get(name)); } 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -