Backend/JAVA2 멘토시리즈

JAVA Eclipse 22 객체 타입 배열

쏠솔랄라 2023. 5. 8. 14:10

 

 

객체 타입 배열

 

: 같은 클래스로 만들어진 변수들의 나열된 집합

같은 구조를 가지고 있으나 다른 객체 값을 저장

 

 

클래스명 객체배열명 [ ] = new 클래스명 [크기];

 

ex.

Aclass ar [ ] = new Aclass[3];

->  배열은 생성되었으나 값은 없는 상태이다

 

 


 

Exercise 1

 

public class ObjectArray {

public static void main(String[] args) {

 

Aclass ar [] = new Aclass [3];

 

ar[0] = new Aclass(); // 객체를 생성해야만 객체 내 필드 접근 가능

 

ar[0].x=100;

ar[0].f1();

 

System.out.println(ar[0].x);

}

 

}

class Aclass {

 

int x;

void f1() {

System.out.println("x: " + x);

}

 

}

 

 


 

 

Exercise 2

 

public class AnimalArray {

 

public static void main(String[] args) {

 

Animal animals[] = new Animal[3];

 

for (int i=0; i<3; i++) {

animals[i] = new Animal();

}

 

animals[0].kind="고양이";

animals[0].name="나르";

animals[0].age=1;

 

animals[1].kind="강아지";

animals[1].name="초코";

animals[1].age=3;

 

animals[2].kind="고양이";

animals[2].name="니코";

animals[2].age=2;

 

for (int i=0; i<3; i++) {

animals[i].info();

}

 

}

}

class Animal {

 

String kind;

String name;

int age;

 

void info() {

System.out.println("kind: " + kind);

System.out.println("name: " + name);

System.out.println("age: " + age);

}

 

}

 

 


 

 

Exercise 3

 

 

import java.util.Scanner;

public class UserArray {

 

public static void main(String[] args) {

 

Scanner sc = new Scanner(System.in);

 

User users [] = new User[4];

 

for (int i=0; i<4; i++) {

users [i] = new User ();

}

 

for(int i=0; i<4; i++) {

System.out.print("name: ");

users[i].name = sc.next();

System.out.print("phone: ");

users[i].phone = sc.next();

System.out.print("age: ");

users[i].age = sc.nextInt();

System.out.print("gender: ");

users[i].gender = sc.next();

}

 

for (int i=0; i<4; i++) {

users[i].info();

}

 

}

}

class User {

 

String name;

String phone;

int age;

String gender;

 

void info() {

 

System.out.println("name: " + name);

System.out.println("phone: " + phone);

System.out.println("age: " + age);

System.out.println("gender: " + gender);

}

}

 

 


 

 

JAVA Eclipse 01 프로그램, 프로그래밍, 기계어, JAVA
https://developernew.tistory.com/63

JAVA Eclipse 02 자바 출력메서드와 입력메서드
https://developernew.tistory.com/71

JAVA Eclipse 03 변수, 자료형, 형변환, 변수의 상수화
https://developernew.tistory.com/74

JAVA Eclipse 04 연산자 정의, 연산자 종류, 연산자 우선순위
https://developernew.tistory.com/78

JAVA Eclipse 05 논리연산자, 비트연산자
https://developernew.tistory.com/80

JAVA Eclipse 06 기타연산자 - 삼항연산자, 대입연산자, 복합대입연산자, instanceof
https://developernew.tistory.com/84

JAVA Eclipse 07 제어문 : 조건문
https://developernew.tistory.com/88

JAVA Eclipse 08 제어문 : 조건문 switch + Random 클래스
https://developernew.tistory.com/93

JAVA Eclipse 09 제어문 : 반복문 for
https://developernew.tistory.com/102

JAVA Eclipse 10 제어문 : 반복문 while, do-while
https://developernew.tistory.com/103

JAVA Eclipse 11 제어문 : 반복문의 break, continue
https://developernew.tistory.com/106

JAVA Eclipse 12 배열 : 배열의 개념 및 사용
https://developernew.tistory.com/107

JAVA Eclipse 13 배열 : 예제풀이, 로또번호 생성기
https://developernew.tistory.com/115

JAVA Eclipse 14 배열 : 다차원배열
https://developernew.tistory.com/117

JAVA Eclipse 15 배열의 복제, for ~ each문
https://developernew.tistory.com/120

JAVA Eclipse 16 카페 주문 시스템(키오스크) 배열과 제어문(반복문, 조건문)으로 풀기
https://developernew.tistory.com/124

JAVA Eclipse 17 method 메서드(메소드)
https://developernew.tistory.com/126

JAVA Eclipse 18 메서드(메소드) 오버로딩
https://developernew.tistory.com/133

JAVA Eclipse 19 객체지향 언어
https://developernew.tistory.com/135

JAVA Eclipse 20 클래스와 객체
https://developernew.tistory.com/155

JAVA Eclipse 21 인스턴스 변수와 클래스 변수
https://developernew.tistory.com/156