일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
- js
- vscode
- string
- 배열
- 자바스크립트
- windows
- javascript
- 이탈리아
- IntelliJ
- html
- CMD
- Eclipse
- 테이블
- json
- 자바
- Array
- CSS
- list
- date
- Visual Studio Code
- input
- Java
- 이클립스
- Maven
- table
- Button
- 문자열
- 인텔리제이
- Files
- ArrayList
- Today
- Total
목록IT/Java (128)
어제 오늘 내일
ArrayList에 새로운 값을 추가, 변경, 삭제하고, 읽어오는 방법입니다. 값 추가하기 - add() 값 변경하기 - set() 값 삭제하기 - remove(), clear() 값 읽기 - get() 1. 값 추가하기 - add() public boolean add(E e) 파라미터로 전달받은 값을 ArrayList의 끝에 추가합니다. public void add(int index, E element) 파라미터로 전달받은 index위치에 element를 추가합니다. 나머지 데이터들은 한 칸씩 다음 index로 이동됩니다. 예제 import java.util.ArrayList; public class ArrayListEx { public static void main(String[] args) { Ar..
ArrayList 객체를 복사하는 방법 2가지를 소개합니다. ArrayList 복사하기 - clone() - Shallow Copy ArrayList 복사하기 - Deep Copy 1. ArrayList 복사하기 - clone() - Shallow Copy public Object clone() 보통 ArrayList를 복사할 때, ArrayList의 clone() 메소드를 사용합니다. 이 메소드는 ArrayList 객체를 shallow copy한 복사본을 리턴합니다. 예제 1 - clone() 후, ArrayList의 데이터 변경 import java.util.ArrayList; public class CloneArrayList { public static void main(String[] args) ..
지난 번에는 ArrayList의 첫번째 또는 마지막 index를 구하는 방법을 알아보았습니다. [Java] ArrayList의 첫번째, 마지막 index 구하기 [Java] ArrayList의 첫번째, 마지막 index 구하기 ArrayList의 첫번째 index는 "0"이고, 마지막 index는 "ArrayList의 길이 - 1" 입니다. 위와 같은 ArrayList가 있고, 이 ArrayList에 5개의 element가 있을 때, ArrayList의 index는 0부터 시작하기 때문에, 첫번.. hianna.tistory.com 이번에는 ArrayList의 첫번째 또는 마지막 index 값을 삭제하는 방법을 소개합니다. remove() ArrayList의 remove() 메소드를 사용하여 첫번째 in..
ArrayList의 첫번째 index는 "0"이고, 마지막 index는 "ArrayList의 길이 - 1" 입니다. 위와 같은 ArrayList가 있고, 이 ArrayList에 5개의 element가 있을 때, ArrayList의 index는 0부터 시작하기 때문에, 첫번째 index는 "0"이고, 마지막 index는 "ArrayList의 길이 - 1"입니다. 코드 import java.util.ArrayList; import java.util.List; public class ArrayListFirstLastIndex { public static void main(String[] args) { // ArrayList 준비 List arrayList = new ArrayList(); arrayList.a..
ArrayList에서 특정값을 삭제하는 방법을 소개합니다. ArrayList.remove() ArrayList.removeAll() Iterator.remove() 1. ArrayList.remove() public E remove(int index) public boolean remove(Object o) ArrayList의 remove() 메소드를 사용하면, 특정 index 또는 특정 값을 ArrayList에서 삭제할 수 있습니다. remove(int index) remove() 메소드의 파라미터로 int를 전달하면, 해당 index의 값이 삭제됩니다. remove(Object o) remove() 메소드의 파라미터로 Object 객체를 전달하면, ArrayList에서 해당 객체를 찾아서 첫번째로..
List에 특정 값이 몇 개나 포함되어 있는지 확인하기 위해 Collections.frequency() 메소드를 사용할 수 있습니다. Collections.frequency() public static int frequency(Collection c, Object o) Collections.frequency() 메소드는 첫번째 파라미터로 받은 Collection 객체에서, 두번째 파라미터로 받은 Object를 찾아서, 그 갯수를 리턴합니다. 코드 import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class CountListElement { public ..
ArrayList의 맨 앞에 element값 또는 새로운 ArrayList를 추가하는 방법입니다. 맨 앞에 값 추가하기 맨 앞에 ArrayList 추가하기 1. 맨 앞에 값 추가하기 public void add(int index, E element) java.util.ArrayList 클래스의 add() 메소드를 이용해서 ArrayList의 원하는 위치에 값을 추가할 수 있습니다. 첫번째 파라미터로, 데이터가 들어가길 원하는 index를 전달하고, 두번째 파라미터로, 입력할 데이터를 전달합니다. 이 메소드를 이용하면, 새로운 데이터를 해당 index에 추가하고, 나머지 데이터들은 다음 인덱스로 이동합니다. 코드 import java.util.ArrayList; import java.util.Arrays..
지난 포스팅에서는 2개의 List를 합치는 방법을 알아보았습니다. [Java] List 2개 합치기
Java에서 2개의 List를 합쳐서 하나로 만드는 방법입니다. List.addAll() Collections.addAll() Stream.of() + flatMap() + collect() 1. List.addAll() boolean addAll(Collection
Java에서 한번 선언된 배열의 길이를 늘리는 것은 불가능합니다. 따라서, 만약 배열의 길이를 늘려서, 원소를 추가해야 한다면 기존의 배열 대신, 새로운 배열을 새로 생성하고(원하는 길이만큼) 기존 배열의 값들을 새로운 배열로 복사해 주어야 합니다. 반복문 Arrays.copyOf() ArrayList 1. 반복문 코드 import java.util.Arrays; public class ArrayLength { public static void main(String[] args) { String[] arr = { "A", "B", "C" }; int arrLength = arr.length; // 기존 배열을 새로운 배열로 복사(길이를 늘려줌) String[] newArr = new String[arrL..