컴퓨터공학 💻 도서관📚
Part2. 5-3 배열(Array) 구현하기 본문
Array 의 특징
- 동일한 데이터 타입을 순서에 따라 관리하는 자료 구조
- 정해진 크기가 있음
- 요소의 추가와 제거시 다른 요소들의 이동이 필요함
- 배열의 i 번째 요소를 찾는 인덱스 연산이 빠름
- jdk 클래스 : ArrayList, Vector
Array 구현하기
public class MyArray {
	int[] intArr;   	//int array
	int count;  		//개수
		
	public int ARRAY_SIZE;
	public static final int ERROR_NUM = -999999999;
	
	public MyArray()
	{
		count = 0;
		ARRAY_SIZE = 10;
		intArr = new int[ARRAY_SIZE];
	}
	
	public MyArray(int size)
	{
		count = 0;
		ARRAY_SIZE = size;
		intArr = new int[size];
	}
	
	public void addElement(int num)
	{
		if(count >= ARRAY_SIZE){
			System.out.println("not enough memory");
			return;
		}
		intArr[count++] = num;		
	}
 
    // 배열 중간에 num 을 추가하는 함수 : 뒤에 요소들을 전부 한칸씩 뒤로 빼는 코드
	public void insertElement(int position, int num)
	{
		int i;
		
		if(count >= ARRAY_SIZE){                          // 추가 불가능(꽉 찬 경우)
			System.out.println("not enough memory");
			return;
		}
		
		if(position < 0 || position > count ){            // index error
			System.out.println("insert Error");
			return;
		}
		
		for( i = count-1; i >= position ; i--){   // count는 클래스의 멤버변수
			intArr[i+1]  = intArr[i];        // 하나씩 이동
		}
		
		intArr[position] = num;
		count++;
	}
	
    
    // 배열 중간 요소를 삭제하는 함수 : 뒤에 요소들을 전부 한칸씩 앞으로 옮기는 코드
    
	public int removeElement(int position)
	{
		int ret = ERROR_NUM;
		 
		if( isEmpty() ){                                 // 삭제 불가능
			System.out.println("There is no element");
			return ret;
		}
		
		if(position < 0 || position >= count ){           //index error
			System.out.println("remove Error");
			return ret;
		}
		
		ret = intArr[position];
		
		for(int i = position; i<count -1; i++ )
		{
			intArr[i] = intArr[i+1];
		}
		count--;
		return ret;
	}
	
	public int getSize()
	{
		return count;
	}
	
	public boolean isEmpty()
	{
		if(count == 0){
			return true;
		}
		else return false;
	}
	
	public int getElement(int position)
	{
		if(position < 0 || position > count-1){
			System.out.println("검색 위치 오류. 현재 리스트의 개수는 " + count +"개 입니다.");
			return ERROR_NUM;
		}
		return intArr[position];
	}
	
	public void printAll()
	{
		if(count == 0){
			System.out.println("출력할 내용이 없습니다.");
			return;
		}
			
		for(int i=0; i<count; i++){
			System.out.println(intArr[i]);
		}
	}
	
	public void removeAll()
	{
		for(int i=0; i<count; i++){
			intArr[i] = 0;
		}
		count = 0;
	}
}public class MyArrayTest {
	public static void main(String[] args) {
		MyArray array = new MyArray();
		array.addElement(10);
		array.addElement(20);
		array.addElement(30);
		array.insertElement(1, 50);
		array.printAll();
		
		System.out.println("===============");
		array.removeElement(1);
		array.printAll();
		System.out.println("===============");
			
		array.addElement(70);
		array.printAll();
		System.out.println("===============");
		array.removeElement(1);
		array.printAll();
		
		System.out.println("===============");
		System.out.println(array.getElement(2));
	}
}// 이 코드는 왜 있는지 모르겠다.
public class MyObjectArray {
	private int cout;
	private Object[] array;
	public int ARRAY_SIZE;
	
	public MyObjectArray()
	{
		ARRAY_SIZE = 10;
		array = new Object[ARRAY_SIZE];
	}
	
	public MyObjectArray(int size)
	{
		ARRAY_SIZE = size;
		array = new Object[ARRAY_SIZE];
	}	
}'✅🌲강의 복습 노트 > 패캠 JavaSpring 강의,코드 복습' 카테고리의 다른 글
| Part2. 5-5 스택(Stack) 구현하기 (0) | 2025.06.23 | 
|---|---|
| Part2. 5-4 연결 리스트 (LinkedList) 구현하기 (0) | 2025.06.21 | 
| Part2. 5-1, 2 여러가지 자료구조에 대해 알아봅시다. (4) | 2025.06.20 | 
| Part2. 4-4 Class 클래스 사용하기 (조금 이해 안 된 강의) (0) | 2025.06.19 | 
| Part2. 4-3 String, StringBuilder, StringBuffer 클래스, text block (0) | 2025.06.19 | 
			  Comments
			
		
	
               
           
					
					
					
					
					
					
				 
								 
								