배열 (Array)
연관된 데이터를 연속적인 형태로 구성된 주고를 가진다.
배열에 포함된 원소는 순서대로 번호(index)가 붙는다.
배열의 크기는 고정되어 있지 않고 가변적인 것을 명심하자!!
index가 number가 아니어도 된다.
자바스크립트의 Array는 HashMap에 가깝다.
배열은 객체로 분류되기 때문에 객체처럼 사용이 가능하다. (하지만 추천하지는 않음)
추가와 삭제가 반복되는 로직이라면 배열 사용은 권장되지 않는다.
배열은 요소를 추가/삭제하고 앞으로 당기고/ 뒤로 미루는데 선형시간이 소요된다.O(n)
배열의 크기는 고정되어 있지 않고 가변적인 것을 명심하자!!
index가 number가 아니어도 된다.
배열은 탐색이 많은경우에 유용함.
//배열 생성
let arr1 = [];
console.log(arr1);
let arr2 = [1, 2, 3, 4, 5];
console.log(arr2);
let arr3 = Array(10).fill(0);
console.log(arr3);
let arr4 = Array.from({ length: 100 }, (_, i) => i);
console.log(arr4);
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/from
// Using an arrow function as the map function to
// manipulate the elements
Array.from([1, 2, 3], x => x + x);
// [2, 4, 6]
// Generate a sequence of numbers
// Since the array is initialized with `undefined` on each position,
// the value of `v` below will be `undefined`
Array.from({length: 5}, (v, i) => i);
// [0, 1, 2, 3, 4]
// 화살표 기능을 지도 기능으로 사용하여
// 요소를 조작하다
배열.from([1, 2, 3], x = > x + x);
// [2, 4, 6]
// 일련의 숫자를 생성
// 배열은 각 위치에서 "undefined"로 초기화되므로,
// 아래의 "v" 값은 "v"가 될 것이다
Array.from({길이: 5}, (v, i) = > i);
// [0, 1, 2, 3, 4]
v는 배열의 값, i는 인덱스로 이해하면 된다!
push, splice
//배열 요소 추가, 삭제
const arr = [1,2,3,];
console.log(arr);
//push 배열 요소 추가
arr.push(4);// 0(1)
arr.push(5,6); // 0(1)
console.log(arr);
// 3번 인덱스에 125를 추가.
arr.splice(3,0,125); // O(n)
console.log(arr);
// 3번 인덱스 값 제거
arr.splice(3,1); // O(n)
console.log(arr[3]);
console.log(arr); // 값 확인
arr.pop(); //마지막 요소 제거
console.log(arr); //값 확인
헷갈리는건 설명 한 번 더
const animals = ['pigs', 'goats', 'sheep'];
const count = animals.push('cows');
console.log(count);
// Expected output: 4
console.log(animals);
// Expected output: Array ["pigs", "goats", "sheep", "cows"]
animals.push('chickens', 'cats', 'dogs');
console.log(animals);
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
// Expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));
// Expected output: Array ["camel", "duck"]
console.log(animals.slice(1, 5));
// Expected output: Array ["bison", "camel", "duck", "elephant"]
console.log(animals.slice(-2));
// Expected output: Array ["duck", "elephant"]
console.log(animals.slice(2, -1));
// Expected output: Array ["camel", "duck"]
console.log(animals.slice());
// Expected output: Array ["ant", "bison", "camel", "duck", "elephant"]
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/push
shift, unshift
const arr = [1,2,3,4,5];
arr.shift(); //맨 앞의 요소 삭제
arr.shift();
console.log(arr);
arr.unshift(8); //맨 앞에 요소 추가
console.log(arr);
join, reverse
const arr = [1, 2, 3, 4, 5, 6];
console.log(arr.join(',')); //콤마로 배열들을 합침
console.log(arr.reverse()); // 배열 거꾸로 바꿈
console.log(arr); //한번사용하면 추후에도 영향이 미치니 조심할 것
concat
const arr1 = [1, 2, 3, 4, 5, 6];
const arr2 = [7,8,9]
console.log(arr1.concat(arr2));
//배열 합치기
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);
console.log(array3);
// Expected output: Array ["a", "b", "c", "d", "e", "f"]
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
for, for of
//배열 순회 방법
const arr = [1, 2, 3, 4, 5];
for (let i = 0; i < 5; i += 1) {
console.log(arr[i]);
}
console.log('--------------------');
for (const item of arr){
console.log(item);
}
'자료구조 | 알고리즘' 카테고리의 다른 글
[자료구조] 큐 (0) | 2023.02.28 |
---|---|
[자료구조]스택 (0) | 2023.01.18 |
[자료구조]연결리스트 (0) | 2023.01.18 |
[알고리즘]시간 복잡도 (0) | 2023.01.18 |
자료구조와 알고리즘의 차이점은? (0) | 2023.01.18 |