using loop to concatenate elements of arrays in Java in a python like way -
i new java. have done python before , concatenating elements of 2 lists or arrays seemed easy loop. how same java??? example, have multidimensional array: //codes
string [][] nameary={{"mr.","mrs.","ms."},{"jones","patel"}};
//the output expecting :
mr. jones, mrs. jones, ms, jones, etc.
// can handpicking elements indices, shown in oracle documentation, looking loop job instead of doing: //code
system.out.println(nameary[0][0]+", "+nameary[1][0]);
` ////so, there way put way in python,i.e.,:
x=["mr.","mrs.","ms."] y=["jonse","patel"] names-[a+b in x b in y]
///this gives me following result:
['mr.jonse', 'mr.patel', 'mrs.jonse', 'mrs.patel', 'ms.jonse', 'ms.patel']
//so, there in java???
you can use loops index variables access array. here i'm using i
loop through first dimension , j
second dimension of string array, while assembling each pair , adding list. part arraylist convenience, can return strings or add them different data structure. hope helps.
edit: explanations
arraylist<string> list = new arraylist<string>(); string [][] nameary={{"mr.","mrs.","ms."},{"jones","patel"}}; for(int = 0;i<nameary[0].length;i++) { for(int j =0;j<nameary[1].length;j++) { list.add(nameary[0][i]+nameary[1][j]); } }
Comments
Post a Comment