자바/list

Algo rhythm·2022년 6월 13일
0

자바

목록 보기
3/6

List

  • 배열과 비슷한 기능을 함.
  • 배열과 달리 유동적인 크기 조정
  • 배열보다 사용되는 빈도가 높음

List 선언(업캐스팅을 통한 객체 생성)

  • List<데이터타입> = new LinkedList<>();
  • List<데이터타입> = new ArrayList<>();
  • List<데이터타입> = new Vector<>();
  • List<데이터타입> = new Stack<>();
  • List는 상위 객체같은 개념으로 생각하고 List의 하위 객체로 LinkedList, ArrayList, Vector, Stack 등이 존재

<데이터 타입>

  • 데이터 타입은 무엇이든 대입될 수 있음
  • String, Integer, Double, Object 등
    -- 여기서 Object는 클래스를 포함, Object는 모든 데이터 타입을 아우르는 개념

인스턴스, 객체 생성 예시

// 출처 : https://velog.io/@alicesykim95/Java-Class-Array-%ED%81%B4%EB%9E%98%EC%8A%A4-%EB%B0%B0%EC%97%B4
package Ex01.classArray;

class Product {
	String name;
	int price;
}

public class ClassArrayEx01 {
	public static void main(String[] args) {
		// 클래스 배열 생성방법	
		Product[] productList = new Product[3];
		
		productList[0] = new Product();
		productList[0].name = "기계식 키보드";
		productList[0].price = 45000;

		productList[1] = new Product();
		productList[1].name = "무소음 마우스";
		productList[1].price = 35000;
		
		Product product = new Product();
		product.name = "장패드";
		product.price = 10000;
		productList[2] = product;
	}
}

Product 클래스를 배열로 구성된 인스턴스를 생성, 배열의 각 원소는 클래스로 Product 클래스의 메소드를 활용

profile
배운 건 써 먹자

0개의 댓글