Java
2023.05.01
연을
2023. 5. 1. 16:58
728x90
객체?
heap? 컴퓨터의 기억 장소에서 그 일부분이 프로그램들에 할당되었다가 회수되는 작용이 되풀이되는 영역.
프로그램 : 값이 들어오고 값이 나오는것
언제 프로세스라 얘기 하는가? RAM에 올라갔을 때
Arraylist → string type1. 시나리오 or 요구사항
1. 고객은 문자를 추가할때 add사용 2. 고객은 문자를 삭제할때 remove사용 3. 고객은 문자를 가져올때 get사용 4. 고객은 ArrayList의 크기를 확인할 때 size사용 5. 고객은 ArrayList에 현재 들어있는 값을 확인 할때 toString사용 |
2. 요구사항 분석
1. lastVal을 전달하면 ArrayList 객체는 add(lastVal)을 수행
4. 고객은 ArrayList의 크기를 확인할 때 size사용
|
3. 클래스 다이어그램
4. ArrayList 자료구조도
add | ![]() |
get | ![]() |
toString | ![]() |
size | this.array.length |
remove | ![]() |
5. 소스
[arraylist]
package arraylist;
public class arraylist {
private String[] array = null;
public boolean add(String lastVal) {
if(this.array != null) {
String[] backup = this.array;
this.array = new String[backup.length+1];
for(int i=0;i<backup.length;i++) {
this.array[i]=backup[i];
}
}else {
this.array = new String[1];
}
this.array[this.array.length-1]=lastVal;
return false;
}
public String get(int index) {
if(this.array == null) {
this.array = new String[1];
}
if(index >= this.array.length) {
throw new IndexOutOfBoundsException();
}
return this.array[index];
}
public String remove(int index) throws IndexOutOfBoundsException{
String result=null;
if(this.array != null) {
if(index < this.array.length) {
String[] backup = this.array;
this.array = new String[backup.length-1];
int nIndex = 0;
for(int i=0;i<backup.length;i++) {
if(i!=index) {
this.array[nIndex]=backup[i];
nIndex++;
}else {
result = backup[index];
}
}
}else {
throw new IndexOutOfBoundsException();
}
}else {
this.array = new String[1];
}
return result;
}
public int size() {
return this.array.length;
}
public String toString() {
String result = "[";
if(this.array != null) {
for(int i=0;i<this.array.length;i++) {
if(i !=0) {
result +=","+this.array[i];
}else {
result += this.array[i];
}
}
}
result +="]";
return result;
}
}
[ArrayListSimnulator]
package arraylist;
public class ArrayListSimulator {
public static void main(String[] args) {
// TODO Auto-generated method stub
arraylist list = new arraylist();
list.add("character to pass");
//list.get(0);
System.out.println(list);
System.out.println(list.get(0));
System.out.println(list.size());
System.out.println(list.toString());
list.add("add");
System.out.println(list.size());
System.out.println(list.get(1));
System.out.println(list);
list.remove(0);
System.out.println(list.size());
System.out.println(list);
}
}
[결과]
[character to pass]
character to pass 1 [character to pass] 2 add [character to pass, add] 1 [add] |
728x90