JAVA 객체지향 프로그래밍 OOP, Class 기본개념 및 예제
https://developernew.tistory.com/21
JAVA 코딩 객체지향프로그래밍 OOP, 클래스 Class, 함수, 메소드
객체 지향 프로그래밍(OOP: Object Oriented Programming) 객체 중심의 프로그래밍 방식 객체끼리의 상호 작용을 통하여 프로그램을 작성하는 방식 부품화 캡슐화 == 클래스 속성과 기능을 하나의 캡슐처
developernew.tistory.com
JAVA 객체지향 프로그래밍 OOP, Class, Static
https://developernew.tistory.com/22
JAVA 코딩 객체지향프로그래밍 OOP, 클래스 Class, static
자바 클래스 기본개념 및 기본예제 https://developernew.tistory.com/21 JAVA 코딩 객체지향프로그래밍 OOP, 클래스 Class, 함수, 메소드 객체 지향 프로그래밍(OOP: Object Oriented Programming) 객체 중심의 프로그래
developernew.tistory.com
public class Array {
// Array라는 클래스를 구현하여 숫자 데이터가 저장되도록 구현
// 단, 자바에서 기본적으로 제공해주는 메소드는 사용하지 않고 구현.
// (구성항목)
// 배열, length(배열의 최대 개수) , size(현재 데이터 개수)
private int[] arr;
private int length;
private int size;
public Array() {
arr = new int[10];
this.length = 10;
}
public Array(int length) {
this.setLength(length);
}
public Array(Array a) {
this.arr = a.getArr();
this.length = a.length;
this.size = a.size;
}
//setter
public void setLength(int length) {
int[] arr = 기존배열복사(length);
this.length = length;
this.size = (this.size > length) ? length : this.size;
this.arr = arr;
}
// 기존 배열 복사
private int[] 기존배열복사(int[] arr) {
//기존 배열이 새로들어온 배열보다 길이가 작은 경우
for(int i = 0; i < this.length; i++) {
arr[i] = this.arr[i];
}
return arr;
}
private int[] 기존배열복사(int length) {
int len = (this.size >= length) ? length : this.size;
int[] arr = new int[length];
for(int i = 0; i < len; i++) {
arr[i] = this.arr[i];
}
return arr;
}
//getter
public int[] getArr() {
int[] arr = new int[this.length];
for(int i = 0; i < size; i++) {
arr[i] = this.arr[i];
}
return arr;
}
public int getLength() {
return length;
}
public int getSize() {
return size;
}
// (기능)
// add(v)
// - v라는 데이터를 마지막칸에 추가
public boolean add(int v) {
if(size == length) {
System.out.println("데이터 공간이 없습니다.");
return false;
}
arr[size] = v;
size++;
return true;
}
// add(i,v)
// - i번째에 v데이터 삽입...
// get(i)
// - 특정 위치의 데이터 추출
// - i번째 데이터 반환
// indexOf(v)
// - 데이터 검색후 데이터 위치반환 - 인덱스값 반환
// remove()
// - 마지막 데이터를 삭제
// - 데이터 없으면 실행 중지
// remove(i)
// - 인덱스 위치의 데이터 삭제
// set(int i , int j)
// - 데이터 서로 교환 , i 와 j 인덱스 위치의 v 교환
// size()
// - 현재 데이터의 개수를 반환
}
package oop.array;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Array arr = new Array();
//arr.setLength(5);
arr.add(1);
arr.add(2);
arr.add(3);
arr.add(4);
arr.add(5);
System.out.println(arr.add(6));
arr.setLength(10);
int[] ar = arr.getArr();
System.out.println(Arrays.toString(ar));
System.out.println(arr.getSize());
System.out.println(arr.getLength());
Array a = new Array(arr);
System.out.println(Arrays.toString(a.getArr()));
System.out.println(a.getSize());
System.out.println(a.getLength());
}
}
'Backend > JAVA' 카테고리의 다른 글
JAVA 코딩 API (Application Programming Interface) - Integer, Exception (0) | 2023.02.23 |
---|---|
JAVA 코딩 Class 클래스 다형성 (0) | 2023.02.23 |
JAVA 코딩 객체지향프로그래밍 OOP, Class 중첩클래스 - 일반중첩클래스, static중첩클래스, 지역중첩클래스, 익명중첩클래스 (0) | 2023.02.21 |
JAVA 코딩 객체지향프로그래밍 OOP, 클래스 Class, static (0) | 2023.02.16 |
JAVA 코딩 객체지향프로그래밍 OOP, 클래스 Class, 함수, 메소드 (0) | 2023.02.16 |