본문 바로가기

Java

[Codility] Lesson 3 : PermMissingElem - JAVA 문제 An array A consisting of N different integers is given. The array contains integers in the range [1..(N + 1)], which means that exactly one element is missing. Your goal is to find that missing element. Write a function: class Solution { public int solution(int[] A); } that, given an array A, returns the value of the missing element. For example, given array A such that: A[0] = 2 A[1] = 3 A[2.. 더보기
[Codility] Lesson 3 : FrogJmp - JAVA 문제 A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. The small frog always jumps a fixed distance, D. Count the minimal number of jumps that the small frog must perform to reach its target. Write a function: class Solution { public int solution(int X, int Y, int D); } that, given three i.. 더보기
[Codility] Lesson 2 : OddOccurrencesInArray - JAVA 문제 A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired. For example, in array A such that: A[0] = 9 A[1] = 3 A[2] = 9 A[3] = 3 A[4] = 9 A[5] = 7 A[6] = 9 the elements at indexes 0 and 2 have value 9, the elements a.. 더보기
[Codility] Lesson 2 : CyclicRotation - JAVA 문제 An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place). The goal is to rotate array A K times; that is, each eleme.. 더보기
[Codility] Lesson 1 - JAVA 문제 A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N. For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binar.. 더보기
‘==’ vs equals() vs hashcode() Java 프로그래밍에서 값이 같은지 비교하려면 어떻게 해야할까요? 숫자나 불리언 타입의 경우에는 ‘==’로 비교하면 되요! 그럼 값이 같은지 확인해주거든요. 그런데 문자열이나 날짜, 파일, 그 외의 객체(Object)를 비교할 때도 ‘==’을 사용하면 될까요? 이런 객체에서 ‘==’을 사용하게 되면 ‘False’가 나올거에요. 왜 그럴까요? 먼저 ‘== ‘ 비교에 대해 알아볼게요. ‘==’은 값을 비교해줘요. 그런데 앞에서 말 했듯이 객체(Object)의 값을 비교할 때에는 false가 나오기도 하죠. 그 이유는 int, byte, short, long, float, double, boolean, char 등 Primitive Type의 변수에 대해서는 저장된 값을 비교하게 되지만 String을 포함한 .. 더보기
[JAVA] UUID 프로젝트를 하다보면 UUID라는 값을 많이 보게 되더라고요. 처음에는 뭐를 의미하는지는 모르겠지만 어떤 ID값인가보다 생각했는데 이번에 UUID에 대해 작성하면서 제대로 공부해볼게요. UUID라는게 뭘까요? 먼저 UUID는 Universal Unique Identifier의 줄임말이에요. 한국말로 하면 범용 고유 식별자 라고 하네요. UUID는 네트워크 상의 개체들을 구분하기 위해 국제 표준(RFC 4122)으로 채택되어 사용하고 있는데요. Java에서는 JDK 1.5부터 지원하고 있어요. 이 UUID를 이용하면 객체를 구분할 수 있는 거의 고유한 값을 생성해준다고 해요. 여기서 거의 고유하다고 말한 이유는 UUID를 생성하다 보면 아주 희박한 확률로 중복된 값이 나올 수도 있기 때문이에요. 저는 여기서.. 더보기
Scanner vs BufferedReader Java에서 표준 입력을 통해 입력을 받는 방법은 뭐가 있을까요? 대표적으로 JDK 1.5에서 제공하는 Scanner 클래스를 이용하는 방법과 JDK 1.1부터 제공하고 있었던 BufferedReader 클래스를 이용하는 방법이 있어요. 물론 그 외의 다양한 방법이 있겠지만 키보드를 통해 입력받는 두 클래스에 대해서 먼저 비교해보도록 할게요. Scanner와 BufferedReader는 어떤 차이가 있을까요? 먼저 입력을 받을 수 있는 자료형의 종류가 달라요. Scanner는 문자(char), 문자열(String), 숫자(int, float, double, ...), 논리형(boolean)등 다양한 형태의 입력을 받을 수 있지만, BufferedReader는 문자열(String)만 입력받을 수 있어요. .. 더보기