main문
import java.util.Arrays;
import java.util.stream.IntStream;
class Main {
public static void main(String[] args) {
new ver2().run();
System.out.printf("\n");
}
}
for문 - 배열연산
class ver1{
public void run(){
int[] arr ={33,2,55,4,51,6,71,18,29,10};
int resultLen=0;
for (int n : arr ) if(n%2!=0) resultLen++;
int[] result = new int[resultLen];
int result_last_index = 0;
for (int n : arr ) {
if (n % 2 != 0) continue;
result[result_last_index++] = n*2;
}
System.out.printf("%s\n", Arrays.toString(arr));
System.out.printf("%s", Arrays.toString(result));
}
}
stream 방식 - 배열연산
class ver2{
public void run() {
int[] arr ={33,2,55,4,51,6,71,18,29,10};
int[] result = Arrays.stream(arr)
.filter(e-> e % 2 == 0)
.map(e -> e * 2)
.sorted()
.toArray();
System.out.printf("%s\n", Arrays.toString(arr));
System.out.printf("%s", Arrays.toString(result));
}
}