본문 바로가기

카테고리 없음

Array 뿌시기 - Leetcode Arrays 101 시작(7/9)

리트코드에는 Arrays 101이라는 수업이 있다.

https://leetcode.com/explore/learn/card/fun-with-arrays/

 

Explore - LeetCode

LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.

leetcode.com

오늘은 Arrays 101의 37개를 Crash 해보려고 한다.

이 course는 아이러니하게도 JAVA로 진행되어 있다고 하는데, 개념을 배우는 것이 중요하기 때문에 우선은 도전해보려고 한다. 총 6개의 Chpater로 이루어져 있으며 간단하게 요약을 남기려고 한다.

 

1. Introduction: Array란?

"Arrays are a simple data structure for storing lots of similar items."라고 설명이 되어있다.

여기서 포인트는 Array 목적이 정보 저장이라는 것과, similar items 즉, 비슷한 것들을 저장한다는 것이다.

더 짧은 문장으로 말해보면 "Array is a collection of items"라고 요약할 수 있다. 

여기서 items에 해당하는 것으로는 integers, strings, DVDs, games, 등등 모든 동질한 물건들이 가능하다.

 

이 챕터에서는 DVD를 저장하는 방법(real-world problem)을 JAVA로 풀고자 한다. 

// The actual code for creating an Array to hold DVD's
DVD[] dvdCollection = new DVD[15]

// A simple definition for a DVD.
public class DVD {
	public String name;
    public int releaseYear;
    public String director;
    
    public DVD(String name, int releaseYear, String director) {
    	this.name = name;
        this.releaseYear = releaseYear;
        this.director = director;
    }
    
    public String toString(){
    	return this.name + ", directed by " + this.director + ", released in " + this.releaseYear;
    }
}

Array 이름은 dvdCollection이라고 지었고, 15개의 빈 공간이 있도록 만들었다. 처음에는 no DVD인 상황이다.

두개의 가장 주된 연산은 (1) writing elements 그리고 (2) reading elements 이다.

 

(1) Writing Items into an array: Array에 값을 입력하려면 어느 자리인지를 알아야 한다. 각 자리는 0에서부터 N-1까지로 규정되어 있으며 우리는 이 자리를 나타내는 수를 index라고 부른다.

// Firstly, we need to actually create a DVD object for The Avengers.
DVD avengersDVD = new("The Avengers", 2012, "Joss Whedon");

// Next, we'll put it into the 8th place of the Array. Remember, because we 
// started numbering from 0, the index we want is 7.
dvdCollection[7] = avengersDVD;

DVD avengersDVD = new("The Avengers", 2012, "Joss Whedon");
DVD findingDoryDVD = new DVD("Finding Dory", 2016, "Andrew Stanton");
DVD lionKingDVD = new DVD("The Lion King", 2019, "Jon Favreau");

// Put "The Incredibles" into the 4th place: index 3.
dvdCollection[3] = incrediblesDVD;
// Put "Finding Dory" into the 10th place: index 9.
dvdCollection[9] = findingDoryDVD;
// Put "The Lion King" into the 3rd place: index 2.
dvdCollection[2] = lionKingDVD;

 avengersDVD라는 DVD object를 만들때 위에서 정의한대로 name, releaseYear, director라는 세 가지 정보를 저장하였다.

그리고 인텍스 숫자 7을 이용해서 dvdCollection의 8번째 자리에 avengersDVD라는 DVD object 삽입하였다. 

마찬가지로 incrediblesDVD, findingDoryDVD, lionKingDVD라는 object를 만들어 원하는 자리에 삽입하였다.

여기서 포인트는 만약에 우리가 이미 채워진 자리에 새로운 정보를 입력하면 overwrite된다는 것이다.

 

(2) Reading Items from an array: 

// Print out what's in indexes 7, 10, and 3.
System.out.printIn(dvdCollection[7]);
System.out.printIn(dvdCollection[10]);
System.out.printIn(dvdCollection[3]);

// Will print:
// The Avengers, directed by Joss Whedon, released in 2012
// null
// Star Wars, directed by George Lucas, released in 1977

우리가 아직 index 10에 정보를 안넣었기에 null이 튀어나왔다.