Java/Java Concept

[Java] LinkedList

군우 2018. 3. 12. 19:49
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package framework22;
 
import java.util.LinkedList;
 
public class MainTest2 {
 
    public static void main(String[] args)
    {
        LinkedList<String> link = new LinkedList<String>();
        
        for(int a = 0;a<10;a++)
            link.add(a,"test "+(a+1));
        for(int a = 0;a<link.size();a++)
        System.out.println(link.get(a));
    }
}
cs



제네릭으로 linkedList 만듬


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
31
package framework22;
 
class Box<T>
{
    T item;
    public Box<T> next;    // 다음인스턴스 자체를 가리키는 거 만듬
    public void inputStore(T item, Box<String> box1)
    {
        this.item = item;
    }
}
 
public class makeLinkedList {
    public static void main(String[] args)
    {
        Box<String> box1 = new Box<String>();
        box1.next = new Box<String>();
        box1.next.next = new Box<String>();
        box1.next.next.next = new Box<String>();
        
        box1.inputStore("The first", box1);
        box1.next.inputStore("Secend", box1.next);
        box1.next.next.inputStore("Third", box1.next.next);
        box1.next.next.next.inputStore("Fourth"null);  // 마지막
        
        System.out.println("first : "+ box1.item);
        System.out.println("second : "+ box1.next.item);
        System.out.println("third : "+ box1.next.next.item);
        System.out.println("fourth : "+ box1.next.next.next.item);
    }
}
cs