일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 |
- 소프트웨어 설계
- java
- 파일 입출력
- C#
- C언어 소스 코드
- 클래스 다이어그램
- Escort GoF의 디자인 패턴
- StringBuffer 클래스
- math.h
- C언어
- 디딤돌 C언어
- 디딤돌 Java 언어 Part1
- 동적 메모리 할당
- C++
- 소프트웨어 접근성
- 소스 구현
- 소스 파일
- C언어 표준 라이브러리 함수 사용법 가이드
- C# 소스 코드
- C언어 표준 라이브러리 함수 가이드
- 디딤돌 C++
- JAVA 언어
- 무료 Java
- gof의 디자인 패턴
- 소스 코드
- 알고리즘
- C언어 소스
- 설계 패턴
- XML.NET
- String 클래스
- Today
- 21
- Total
- 98,492
프로그램 소스
[Java 소스] 정수 동적 배열, 제네릭 동적 배열 본문
[Java 소스] 정수 동적 배열, 제네릭 동적 배열
//IntDArray.java
//정수 동적 배열
public class IntDArray {
int[] buffer;
int capacity;
int usage;
public IntDArray(int capacity){
this.capacity = capacity;
buffer = new int[capacity];
usage = 0;
}
public boolean isEmpty(){
return usage == 0;
}
public boolean isFull(){
return usage == capacity;
}
public int size(){
return usage;
}
public boolean add(int value){
if(isFull()){
return false;
}
buffer[usage] = value;
usage++;
return true;
}
public void viewAll(){
String outstr = String.format("저장소 크기:%d 보관개수:%d",capacity,usage);
System.out.println(outstr);
for(int i = 0; i<usage;i++){
System.out.print(buffer[i]+" ");
}
System.out.println();
}
}
//DArray.java
//제네릭 동적 배열
public class DArray<datatype> {
Object[] buffer;
int capacity;
int usage;
public DArray(int capacity){
this.capacity = capacity;
buffer = new Object[capacity];
usage = 0;
}
public boolean isEmpty(){
return usage == 0;
}
public boolean isFull(){
return usage == capacity;
}
public int size(){
return usage;
}
public boolean add(datatype value){
if(isFull()){
return false;
}
buffer[usage] = value;
usage++;
return true;
}
public void viewAll(){
String outstr = String.format("저장소 크기:%d 보관개수:%d",capacity,usage);
System.out.println(outstr);
for(int i = 0; i<usage;i++){
System.out.print(buffer[i]+" ");
}
System.out.println();
}
}
//Program.java
//정수 동적 배열과 제네릭 동적 배열
public class Program {
public static void main(String[] args){
System.out.println("==Test IntDArray===");
IntDArray idarr = new IntDArray(10);
idarr.viewAll();
idarr.add(3);
idarr.viewAll();
idarr.add(2);
idarr.viewAll();
idarr.add(6);
idarr.viewAll();
System.out.println("==Test DArray<integer>===");
DArray<Integer> darr = new DArray<Integer>(10);
darr.viewAll();
darr.add(3);
darr.viewAll();
darr.add(2);
darr.viewAll();
darr.add(6);
darr.viewAll();
System.out.println("==Test DArray<String>===");
DArray<String> darr2 = new DArray<String>(10);
darr2.viewAll();
darr2.add("Hello");
darr2.viewAll();
darr2.add("언제나 휴일");
darr2.viewAll();
darr2.add("ehpub.co.kr");
darr2.viewAll();
}
}실행 결과
==Test IntDArray===
저장소 크기:10 보관개수:0
저장소 크기:10 보관개수:1
3
저장소 크기:10 보관개수:2
3 2
저장소 크기:10 보관개수:3
3 2 6
==Test DArray<integer>===
저장소 크기:10 보관개수:0
저장소 크기:10 보관개수:1
3
저장소 크기:10 보관개수:2
3 2
저장소 크기:10 보관개수:3
3 2 6
==Test DArray<String>===
저장소 크기:10 보관개수:0
저장소 크기:10 보관개수:1
Hello
저장소 크기:10 보관개수:2
Hello 언제나 휴일
저장소 크기:10 보관개수:3
Hello 언제나 휴일 ehpub.co.kr
본문
'Java > 디딤돌 Java 언어 Part2' 카테고리의 다른 글
[Java 소스] Comparable 인터페이스로 한정한 제네릭 메서드 Sort (0) | 2017.01.21 |
---|---|
[Java 소스] 제네릭 메서드 (0) | 2017.01.20 |
[Java 소스] 정수 동적 배열, 제네릭 동적 배열 (0) | 2017.01.20 |
[Java 소스] 사용자 정의 예외 클래스 (0) | 2017.01.20 |
[Java 소스] finally 문 사용 예 (0) | 2017.01.19 |
[Java 소스] throw 문 사용 예 (0) | 2017.01.19 |