티스토리 뷰

반응형

기존 For 문: for(초기화; 조건; 증감) => 하나의 변수(i)로 그 값을 count 한다.

 for (int i = 0; i < measureNumCount; i++) {
            int inputMeasureNum = sc.nextInt();
            findMeasure.inputMeasureToTheList(inputMeasureNum);
        }

 

향상된 For 문: for(대입받을 변수 정의 : 배열명)

* 배열/Colletction의 저장된 elements를 읽어오는 용도로만 사용 가능

* 배열의 자료형(①)과 변수 타입(②)이 같아야 한다.

* 배열의 크기만큰 for문 수행 -> size 따로 지정x

 

 

1) 배열(①)에서 가져올 첫번째 값이 존재하는지 확인

2) 값이 있다면 -> 변수(②)에 저장

2-1) 값이 없다면 -> for문 종료

3) 실행문(③) 실행

 

 

 

 

 

 

 

 

 

 

 

 

[Ex1] 기본 향상된 For문(배열)

int [ ] numList = {1, 2, 3, 4, 5};

for( int i=0; i<numList.length; i++){

System.out.println(numList[i]);

}

 

for( int tmp : numList ){

System.out.println(tmp);

}

 

[Ex2] Class Object 사용하기 (Class : SortLocation)

 SortLocation[] sortLocations = new SortLocation[countLocation];   

 for (SortLocation sortLocation : sortLocations) {
            System.out.println(sortLocation.getX() + " " + sortLocation.getY());
        }

 

[Ex3] Collection 객체 사용하기 (Class: Employee)

Employee list에서 동일한 이름을 찾을 경우

List<Employee> empList = new ArrayList<Employee>();

Set<Employee> empSet = new HashSet<Employee>();

Map<Employee, Integer> empSameNameMap = new HashMap<Employee, Integer>();

 

Employee e1 = new Employee("AAA", 1, "Company1", 24);

Employee e2 = new Employee("AAA", 1, "Company1", 24);

 

  empList.add(e1);

  empList.add(e2);

  int frequency=1;

 

 

for (Employee obj : empList) {

            if (empSameNameMap.containsKey(obj.getEmployeeName())) {

                empSameNameMap.put(obj, frequency + 1);

            } else {

                empSameNameMap.put(obj, frequency);

            }

           

            for (Map.Entry<Employee, Integer> entry : empSameNameMap.entrySet()) {

                System.out.println("Key = " + entry.getKey().getEmployeeName()

                        + ", Value = " + entry.getValue());

            }

   }

 

[Ex4] ArrayList에서 객체의 index 가져오기- index 변수를 이용 (Class : SortLocation)

ArrayList<SortLocation> sortLocations = ArrayList<SortLocation>();

ArrayList<Integer> addIndexList = new ArrayList<Integer>();

 

int index = 0;

for(SortLocation element : sortLocations){

   if(element.x == 3){

      addIndexList.add(index);

    }

    index++;

}

 

[Ex5] ArrayList에서 객체의 index 가져오기- indexOf 함수를 이용 (Class : SortLocation)

ArrayList<SortLocation> sortLocations = ArrayList<SortLocation>();

ArrayList<Integer> addIndexList = new ArrayList<Integer>();

 

for(SortLocation element : sortLocations){

   if(element.x == 3){

      addIndexList.add(sortLocations.indexOf(element));

    }  

}

 

Q) Collection : List, Map, Set 

 

 

 

출저) https://kingpodo.tistory.com/55, https://twpower.github.io/54-get-index-in-java-iterator, https://coderanch.com/t/643629/java/Grouping-employees-names-employee-list

반응형

'JAVA' 카테고리의 다른 글

[JAVA] 객체지향설계 5원칙 - SOLID  (0) 2022.11.07
[JAVA] Stream  (0) 2022.09.14
ArrayList 정렬 : Comparable 과 Comparator  (0) 2020.01.06
객체 배열이란(클래스를 배열로 선언하기)  (0) 2020.01.03
StringTokenizer  (0) 2020.01.03
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함