자료구조

Array

밍웨이 2021. 1. 6. 23:27
728x90

- 논리적 저장순서와 물리적 저장순서가 일치한다.

- 특정 자료형들이 메모리 공간상에서 연속적으로 저장되어 있다.

- immutable하다.

- 인덱스로 해당 원소로 접근 가능하다.

 

 

검색

- 시작복잡도: O(1)

 

삽입

- 시작복잡도O(n)

-  해당원소에 접근하여 삽입 후 Shift

- 모든 공간이 꽉 차면 새로운 메모리 공간을 할당받아 옮겨야 한다.

 

삭제

- 시작복잡도O(n)

-  해당원소에 접근하여 삭제 후 Shift

 

메모리 할당

- compile time에 할당되어 진다.

- 정적 메모리 할당

 

 

데이터에 접근이 많다면 Array를 사용하자

 

 

배열 선언후 출력

class Array {
	public static void main(String[] args) {
    	int[] a = {1, 2, 3, 4, 5};
        
        for (int i = 0; i < a.length; i ++)
        System.out.println(a[i]);
    }
}

 

 

배열 뒤집기

package revers;

public class Main {

    static void swap(int[] array, int first, int second) {
        int target = array[first];
        array[first] = array[second];
        array[second] = target;
    }

    static void revers(int[] array) {
        for (int i = 0; i < array.length / 2; i++)
            swap(array, i, array.length - i - 1);
    }

    public static void main(String[] args) {
        int[] target = { 1, 2, 3, 4, 5 };

        for (int i = 0; i < target.length; i++)
            System.out.println(target[i]);

        revers(target);

        System.out.println("-----역정렬----");
        for (int i = 0; i < target.length; i++)
            System.out.println(target[i]);
    }
}

'자료구조' 카테고리의 다른 글

asymptotic runtime, big-O  (0) 2021.05.27
Linked List  (0) 2021.01.11