Test test1, Test test2, Test test3...Test test i
반복된 이름의 객체를 만들고 싶을 때
for문 i를 이용할 수 있을까?
public class Test {
int num;
String str;
public Test() {
}
public void makeTests (int num) {
for (int i = 1; i <= num ; i++) {
Test "test" + i = new Test();
}
}
}
위 코드처럼
for문에 "test" + i 형식으로 객체 이름을 짓는 것은 불가능하다.
public class Test {
int num;
String str;
public Test() {
}
public void makeTests (int num) {
Test[] testArray = new Test[num];
for (int i = 0; i < num +1 ; i++) {
testArray[i] = new Test();
}
}
}
대신 위 예시처럼
배열이나 Array를 이용할 수 있다.
'JABA' 카테고리의 다른 글
| 16. stream을 이용하여 객체의 특정 요소를 기준으로 정렬하기 (0) | 2023.04.07 |
|---|---|
| 13. 객체지향 (1) (0) | 2023.04.04 |
| 12. static (0) | 2023.03.24 |
| 11. 생성자와 Method 메서드 (0) | 2023.03.24 |
| 10. format 출력 printf (0) | 2023.03.24 |