Java
Java Datastructure
이무쿤
2020. 10. 10. 20:30
반응형
import java.util.*;
stack
Stack<Integer> sta = new Stack<Integer>();
sta.push(1);
sta.push(2);
while(!sta.isEmpty())
{
//peek() 맨 위 객체 반환
//pop() 맨 위 삭제하면서 반환
System.out.println(sta.pop());
}
Queue<Integer> que = new LinkedList<Integer>();
que.offer(1);
que.offer(2);
que.offer(3);
while(!que.isEmpty())
{
//poll() 삭제하면서 반환
//peek() 가장 처음 반환
System.out.println(que.poll());
}
ArrayList
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
//size()로 크기 구할 수 있음
System.out.println(list.size());
//0번 인덱스 출력
System.out.println(list.get(0));
for(Integer i : list) { //for문을 통한 전체출력
System.out.println(i);
}
//배열처럼 사용
ArrayList<Integer>[] arr = new ArrayList[20001];
//선언 후에 생성해야 함.
for(int i = 0; i < n; i++)
arr[i] = new ArrayList<Integer>();
priority_queue
PriorityQueue<Integer> pr = new PriorityQueue<Integer>();
pr.offer(1);
pr.offer(2);
pr.offer(3);
//queue와 같음
while(!pr.isEmpty())
{
//poll() pr 처음 값 반환하고 제거
//peek() 제일 위의 값
System.out.println(pr.poll());
}
HashMap
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
map.put(1,1);
map.put(32,0);
map.put(3,1);
//key값으로 value 출력
System.out.println(map.get(32));
//key값 제거
map.remove(1);
//없는 key 참조 시에 null임
System.out.println(map.get(1));
public class Obj implements Comparable<Obj>
{
int a;
int b;
public Obj(int a, int b)
{
this.a = a;
this.b = b;
}
//1 참 -1 거짓
@Override
public int compareTo(Obj obj)
{
if(this.a < obj.a)
return 1;
else
return -1;
}
}
반응형