简体   繁体   中英

Printing out specific elements from different arrays in Java

Suppose I started off with the following code you see below in java:

int x[] = {234, 345, 348, 456, 567, 765, 654, 544, 587, 578};
int y[] = {456, 456, 345, 347, 346, 467, 562, 562, 532, 576};
int z[] = {345, 324, 312, 317, 304, 305, 307, 308, 309, 298};

As you can see, there are 3 different arrays, each with 10 different elements. I want the first elements from each array to be printed together. Then I want the second elements to be printed together and so on and so forth. Essentially, I want the output to look like this:

  (234, 456, 345)
  (345, 456, 324)
  (348, 345, 312)
  (456, 347, 317)
  (567, 346, 304)
  (765, 467, 305)
  (654, 562, 307)
  (544, 562, 308)
  (587, 532, 309)
  (578, 576, 298)

I know that by doing

System.out.println (x[0]); 

I can get the first element to be printed out. However, I want to know how to do this using the starting code shown above, and I know that my method is not very efficient.

for(int i=0;i<x.length;i++){
 System.out.println ("("+x[i]+ " ,"+y[i]+" ,"+z[i]+")");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM