[java] Apache Commons Lang 을 사용하여 배열의 유효성을 체크하는 방법

배열을 다루는 프로그래밍에서 배열의 유효성을 체크하는 것은 중요한 작업 중 하나입니다. Apache Commons Lang라는 라이브러리는 자바에서 배열 유효성 체크를 훨씬 간편하게 할 수 있게 도와줍니다.

Apache Commons Lang 라이브러리는 Maven 또는 Gradle을 통해 프로젝트에 의존성을 추가하여 사용할 수 있습니다.

Apache Commons Lang 의존성 추가

Maven을 사용하는 경우

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

Gradle을 사용하는 경우

implementation 'org.apache.commons:commons-lang3:3.12.0'

라이브러리를 프로젝트에 추가한 후, 배열의 유효성을 체크하는 방법을 알아보겠습니다.

배열 유효성 체크하기

Apache Commons Lang을 사용하여 배열의 유효성을 체크하는 방법은 다양합니다. 예를 들어 다음과 같은 체크를 할 수 있습니다.

배열이 널(null)인지 체크하기

import org.apache.commons.lang3.ArrayUtils;

int[] array = null;

if(ArrayUtils.isEmpty(array)) {
    System.out.println("배열이 널입니다.");
}

배열이 비어있는지 체크하기

import org.apache.commons.lang3.ArrayUtils;

int[] array = {};

if(ArrayUtils.isEmpty(array)) {
    System.out.println("배열이 비어있습니다.");
}

배열의 길이 체크하기

import org.apache.commons.lang3.ArrayUtils;

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

if(ArrayUtils.isNotEmpty(array)) {
    System.out.println("배열의 길이는 " + array.length + " 입니다.");
}

배열에서 특정 값이 있는지 체크하기

import org.apache.commons.lang3.ArrayUtils;

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

if(ArrayUtils.contains(array, targetValue)) {
    System.out.println("배열에 " + targetValue + "가 포함되어 있습니다.");
}

마무리

Apache Commons Lang을 사용하면 배열의 유효성을 체크하는 작업을 훨씬 쉽게 할 수 있습니다. 이 글에서는 배열이 널인지, 비어있는지, 길이가 있는지, 특정 값이 있는지 체크하는 방법을 살펴보았습니다.

더 자세한 내용은 Apache Commons Lang 공식 문서를 참조하시기 바랍니다.