02.アレイ(Array)


Array特性

  • 同じデータ型のデータ構造を順番に管理

  • 固定サイズ

  • 要素の追加と削除時に他の要素を移動する必要があります

  • 配列内のi番目の要素を検索するインデックス演算が速い

  • jdkクラス:ArrayList、Vector
  • Arrayの実装
    MyArray.java
    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;
    				
    	}
    
    	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--){
    			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;
    	}
    }
    MyArrayTest.java
    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));
    		
    	}
    }
    MyObjectArray.java
    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];
    	}
    	
    	
    
    	
    	
    	
    }