[Dairy] S.C.C 4일차 / 배열

Shaun.the.sheep·2025년 6월 25일

[Dairy] S.C.C TIL

목록 보기
3/22
post-thumbnail

C#에는 List의 형태의 동적 배열이 존재한다.

자바는 같은 기능을 하는 ArrayList를 제공한다.

동적 배열

ArrayList

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

        for (int sLoop =0 ; sLoop < 10 ; sLoop ++)
            sNums.add(sLoop);

        int sidx = sNums.indexOf(6);
        System.out.println("index has been located in No." + sidx);
  • 배열처럼 인덱스를 통해 데이터를 가져올 수도 있고 배열 내에 존재하는 데이터 조회도 가능하다.

HashSet

        HashSet<String> tempHashSet = new HashSet<String>();

        tempHashSet.add("Robin");
        for (int sLoop =0 ; sLoop < 10 ; sLoop ++)
            tempHashSet.add(sLoop);
  • 중복 값 허용이 불가하고 순서 또한 보장 되지 않는다는 특징이 있다.
  • 중복 값이 보장이 안되기 때문에 인덱스로 데이터 반환이 불가 하다.

HashMap

        HashMap<String, Integer> tempMap = new HashMap<>();

        tempMap.put("Robin",31);
        tempMap.put("Soo",34);
        tempMap.put("Sparta",32);
        tempMap.put("Jonny",37);
        tempMap.put("Roow",35);
        System.out.println("tempMap`s Datas = "+ tempMap);
        System.out.println("tempMap`s Keys = "+ tempMap.keySet());
        System.out.println("tempMap`s Values = "+ tempMap.values());
  • c#의 dictionary와 같은 기능으로 사용한다.

0개의 댓글