import java.util.ArrayList;
public class ListEx1 {
public static void main(String[] agrs){
ArrayList ar1 = new ArrayList();
ArrayList ar2 = new ArrayList(100);
ar1.add(1);
ar1.add(new Integer(2));
//데이터의 갯수
System.out.println("ArrayList 사이즈 : " +ar1.size());
for(int i=0; i<ar1.size(); i++){
System.out.println(ar1.get(i));
}
}
}
import java.util.*;
public class ArrayListEx1 {
public static void main(String[] args){
ArrayList list1 = new ArrayList(10);
list1.add(new Integer(5));
list1.add(new Integer(4));
list1.add(new Integer(2));
list1.add(new Integer(0));
list1.add(new Integer(1));
list1.add(new Integer(2));
ArrayList list2 = new ArrayList(list1.subList(1,4));
print(list1, list2);
Collections.sort(list1);
Collections.sort(list2);
System.out.println("list1.containsAll(list2) : " +list1.containsAll(list2));
list2.add("B");
list2.add("C");
list2.add(3, "A");
print(list1, list2);
System.out.println("list1.retainAll(list2) :" +list1.retainAll(list2));
print(list1, list2);
for(int i=list2.size()-1; i>=0; i--){
if(list1.contains(list2.get(i)))
list2.remove(i);
}
print(list1, list2);
}
static void print(ArrayList list1, ArrayList list2){
System.out.println("list1:"+list1);
System.out.println("list1:"+list2);
System.out.println();
}
}
package p1;
import java.util.*;
public class VectorEx1 {
public static void main(String[] args){
Vector v = new Vector(5);
v.add("1");
v.add("2");
v.add("3");
v.add(v);
print(v);
v.trimToSize();
System.out.println("=== After trimToSize() ===");
print(v);
v.ensureCapacity(6);
System.out.println("=== After ensureCapacity(6) ===");
print(v);
v.setSize(7);
System.out.println("=== After setSize(7) ===");
print(v);
v.clear();
System.out.println("=== After clear() ===");
print(v);
}
public static void print(Vector v){
System.out.println(v);
System.out.println("size : " +v.size());
System.out.println("capacity : " +v.capacity());
}
}
import java.util.*;
public class ListLinkedListTest {
public static void main(String[] args){
ArrayList a1 = new ArrayList(1000000);
LinkedList l1 = new LinkedList();
System.out.println("==순차적으로 추가하기==");
System.out.println("ArrayList : " +add1(a1));
System.out.println("LinkedList : " +add1(l1));
System.out.println();
System.out.println("==중간에 추가하기==");
System.out.println("ArrayList : " +add2(a1));
System.out.println("LinkedList : " +add2(l1));
System.out.println();
System.out.println("==순차적으로 삭제하기==");
System.out.println("ArrayList : " +remove1(a1));
System.out.println("LinkedList : " +remove1(l1));
}
public static long add1(List list){
long start = System.currentTimeMillis();
for(int i=0; i<100000; i++) list.add(i+"");
long end = System.currentTimeMillis();
return end -start;
}
public static long add2(List list){
long start = System.currentTimeMillis();
for(int i=0; i<1000; i++) list.add(500, "X");
long end = System.currentTimeMillis();
return end -start;
}
public static long remove1(List list){
long start = System.currentTimeMillis();
for(int i=list.size()-1; i>0; i--) list.remove(i);
long end = System.currentTimeMillis();
return end -start;
}
public static long remove2(List list){
long start = System.currentTimeMillis();
for(int i=0; i<1000; i++) list.remove(i);
long end = System.currentTimeMillis();
return end -start;
}
}
import java.util.*;
public class StackQueueEx {
public static void main(String[] args){
Stack st = new Stack();
Queue q = new LinkedList();
st.push("0");
st.push("1");
st.push("2");
q.offer("0");
q.offer("1");
q.offer("2");
System.out.println("= Stack =");
while(!st.empty()){
System.out.println("출력값: " +st.pop() +" 남은 공간: " +st.size());
}
System.out.println("= Queue =");
while(!q.isEmpty()){
System.out.println("출력값: " +q.poll() +" 남은 공간: " +q.size());
}
}
}
import java.util.*;
public class IteratorEx1{
public static void main(String[] args){
Object[] objArr = {"1", new Integer(1), "2", "2", "3", "3", "4", "4", "4" };
// Set set = new HashSet();
Set set = new LinkedHashSet();
for(int i=0; i<objArr.length; i++){
set.add(objArr[i]);
}
System.out.println(set);
}
}
import java.util.*;
public class HashMapEx2 {
public static void main(String[] args){
HashMap map = new HashMap();
map.put("김자바", new Integer(90));
map.put("김자바", new Integer(100));
map.put("이자바", new Integer(100));
map.put("강자바", new Integer(80));
map.put("안자바", new Integer(90));
Set set = map.entrySet();
Iterator it = set.iterator();
while(it.hasNext()){
Map.Entry e = (Map.Entry)it.next();
System.out.println("이름 : " +e.getKey() +", 점수 : " +e.getValue());
}
set = map.keySet();
System.out.println("참가자 명단 : " +set);
Collection values = map.values();
it = values.iterator();
int total = 0;
while(it.hasNext()){
Integer i = (Integer)it.next();
total += i.intValue();
}
System.out.println("총점 : " +total);
System.out.println("평균 : " +(float)total/set.size());
System.out.println("최고점수 : " +Collections.max(values));
System.out.println("최저점수 : " +Collections.min(values));
}
}
'Study > Programming' 카테고리의 다른 글
AWT (0) | 2009.05.18 |
---|---|
자바 달력 (0) | 2009.05.18 |
자바 예외처리 (0) | 2009.05.18 |
자바스크립트 ID 저장, 중복체크 (0) | 2009.05.16 |
자바스크립트 달력 (0) | 2009.05.15 |