![](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fbbv5rh%2FbtqWVRAzlb5%2Fv70rtRRG34e8AS23VG8kuk%2Fimg.png)
[Java] 자바 배열 전체 출력하는 5가지 방법 (Arraylist이용) : for문, iterator 사용
필그램
·2017. 8. 15. 02:31
배열을 전체 보는 5가지 방법을 정리해 봅니다.
5가지 자바 배열을 확인해 보는 방법은 아래와 같습니다.
- For Loop (for 문)
- Advanced For Loop (상급 for 문)
- Iterator
- While Loop
- Collections’s stream() util (Java8)
이와 같이 5가지 방법이 있습니다.
이 다섯가지 방법을 이용하면, 자바프로그램으로 for문에 대한 이해와 배열, while문 등을 한번에 연습할 수 있습니다.
일단 자바에서 Main과 Employee 라는 클래스를 만듭니다.
이 클래스는 '이름'과 '나이'만 필드로 갖습니다.
보통 배열을 쉽게 String하나만 넣는데, 좀 다양한 객체를 넣어보겠습니다. (클래스 연습을 위해 중급용으로 클래스를 나누었습니다.)
간단하지만, 전체적으로 알아놓으면 필요할때 적절히 사용할 수 있습니다.
package NewHR;
public class Employee {
String name;
int age;
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + "]";
}
}
자바 배열의 Main 클래스는 아래와 같습니다.
package NewHR;
import java.util.ArrayList;
import java.util.Iterator;
public class HRMain {
public static void main(String[] args) {
// 3. Arraylist 배열 추가. 총 4개의 객체를 배열에 넣습니다. 2가지 방법으로 넣었으니, 참고하시면 됩니다.
ArrayList<Employee> employees3 = new ArrayList<Employee>();
employees3.add(new Employee("Siwon3", 33));
employees3.add(new Employee("John3", 22));
Employee e3 = new Employee("Ric3", 20);
Employee e4 = new Employee("Jake3", 29);
employees3.add(e3);
employees3.add(e4);
// 자바배열의 5가지 방법 한가지씩 보여줍니다.
//0. 인덱스를 통한 하나씩 조회
System.out.println("0. 인덱스를 통한 하나씩 조회");
String emp0 = employees3.get(0).toString();
// String emp1 = employees3.get(1).toString();
// String emp2 = employees3.get(2).toString();
System.out.println("인덱스를 통한 하나씩 조회: " + emp0);
// 1.기본 for문
System.out.println("\n 1. 기본 for문을 통한 전체 조");
for (int i=0; i < employees3.size(); i++) {
System.out.println(employees3.get(i));
}
// 2.Iterator문
System.out.println("\n 2.Iterator 통한 전체 조회");
Iterator<Employee> empIterator = employees3.iterator();
while (empIterator.hasNext()) {
System.out.println(empIterator.next());
}
// 3. New for-loop문
System.out.println("\n 3. New for-loop 통한 전체 조회");
for(Object object : employees3) {
System.out.println(object);
}
// 4. while loop문
System.out.println("\n 4. while loop 통한 전체 조회");
int i = 0;
while ( i < employees3.size()) {
System.out.println(employees3.get(i));
i++;
}
// 5. collection stream()문
System.out.println("\n 5. collection stream()");
employees3.forEach((temp) -> {
System.out.println(temp);
});
}
}
자바 배열별 출력결과
0. 인덱스를 통한 하나씩 조회
인덱스를 통한 하나씩 조회: Employee [name=Siwon3, age=33]
1. 기본 for문을 통한 전체 조회
Employee [name=Siwon3, age=33]
Employee [name=John3, age=22]
Employee [name=Ric3, age=20]
Employee [name=Jake3, age=29]
2.Iterator 통한 전체 조회
Employee [name=Siwon3, age=33]
Employee [name=John3, age=22]
Employee [name=Ric3, age=20]
Employee [name=Jake3, age=29]
3. New for-loop 통한 전체 조회
Employee [name=Siwon3, age=33]
Employee [name=John3, age=22]
Employee [name=Ric3, age=20]
Employee [name=Jake3, age=29]
4. while loop 통한 전체 조회
Employee [name=Siwon3, age=33]
Employee [name=John3, age=22]
Employee [name=Ric3, age=20]
Employee [name=Jake3, age=29]
5. collection stream()
Employee [name=Siwon3, age=33]
Employee [name=John3, age=22]
Employee [name=Ric3, age=20]
Employee [name=Jake3, age=29]
'프로그래밍 > 모바일: iOS, Java, Android, Swift' 카테고리의 다른 글
[iOS] segment(세그먼트) 간략소스 (0) | 2017.08.19 |
---|---|
[iOS] 데이터 픽커(Data Picker) 만들기 (0) | 2017.08.17 |
[iOS 기초] 텍트스 입력 후, 키보드 없애기 (0) | 2017.08.14 |
[iOS] Xcode 시뮬레이터에서 키보드 보는법 (0) | 2017.08.14 |
[Java] ArrayList 이해하기 (0) | 2017.08.09 |