본문 바로가기
비트 단기/java

java LInkedList 후~

by woohyun22 2018. 12. 4.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package Pack01;
import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
class Apple{
    int a,b;
    Apple(){
        //System.out.println("생성자콜");
    }
    void print() {
        System.out.println(a+" "+b);
    }
}
public class Hello {
    public static void main(String[] args) {
        LinkedList<Apple> ll = new LinkedList<>();
        //<>컨테이너 안에는 함수이름이 들어감 모든걸 넣을 수 있다.
        Apple apple = new Apple();
 
        ll.add(apple);
        //add 정수 - 숫자,문자 - 문자열, apple - apple
        ll.add(new Apple());//객체가 바로 생성되는 코드
 
        System.out.println(ll.size());
 
        //세가지방법
 
        System.out.println(ll);
        for (int i = 0; i < ll.size(); i++) {    
            Apple n2 = ll.get(i);
            //앞에 타입을 linkedlist 타입에 맞춰 줘야된다.
            n2.print();
            //Apple안의 a,b 값이 나온다 -지금은 default값
        }
        System.out.println();
        for (int j = 0; j < ll.size(); j++) {
            ll.get(j).print();//체이닝을 걸어서 사용가능하다.
        }
        System.out.println();
        for (Apple i : ll) {
            i.print();//foreach 앞의 i , 뒤의 ll은 자동으로 size값
        }
        System.out.println();
        
        for (int i = 0; i < 5; i++) {//얘들은 행으로 작용한다.
            Apple app = new Apple();
            app.a = i*2+0;
            app.b = i*2+1;
            ll.add(app);
            System.out.println(app.a+ " "+app.b);
        }
        System.out.println();
        
        for (Apple i : ll) {//이 i는 행으로 작용한다.
            i.print();
        }
        //01
        
        System.out.println();
        Apple a2 = new Apple();//값 두개
        a2.a = 88;
        a2.b = 99;
        //순서대로
        ll.set(2, a2);//set(n,n2)n번째 행에 추가 n2값들을 추가
        for (Apple i : ll) {
            i.print();
        }
        
        System.out.println();
        
        ll.remove(1);//remove(n)n번째 행을 삭제
        
        for (Apple i : ll) {
            i.print();
        }
        
        System.out.println();
        //맨위에 00들이 뜨는것은 위에서 추가로 넣어놓았기 때문
        
        //LinkedList를 활용해서 은행 입금 알고리즘을 풀려고했었다~
        
    }
}
cs




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
32
33
34
35
package Pack01;
import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
class Apple{
    int a,b;
    int c = 100;
    Integer d;//객체 생성이 되야 사용가능하다. class이므로
    Integer e = new Integer(100);
    
    String f;
    String g = new String();
    
    //많이 틀리는 부분
    //a++;   변수 선언,변수 초기화를 제외한 다른 실행코드는 넣을 수 없다.
    //b=200; 안됨 - 변수를 선언과 동시에 초기화하는 코드는 가능하지만
    //d = new Integer(200);
    
    //생성자 또는 함수 안에서 쓰면 가능한 코드들이다.
    
    Apple(){
        //System.out.println("생성자콜");
    }
    void print() {
        System.out.println(a+" "+b);
    }
}
public class Hello {
    public static void main(String[] args) {
        
    }
}
cs




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
package Pack01;
import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
class Apple{//부모
    void func01() {
        System.out.println("호랑이");
    }
}
class Orange extends Apple{//자식//extends Apple상속관계
    void func02() {//class 자식 extends 부모
        System.out.println("코끼리");
    }
}
//원래 만들어진 class = 부모
//
public class Hello {
    public static void main(String[] args) {
    /*Apple a1 = new Apple();    
    a1.func01();*/
    Orange o1 = new Orange();
    o1.func02();
    o1.func01();//부모 클래스에 있는 func01()을 출력할 수 있다.
    }
}
cs



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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package Pack01;
import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
class Apple{//부모
    void func01() {
        System.out.println("호랑이");
    }
    void func03() {
        System.out.println("부모 독수리");
    }
}
class Orange extends Apple{//자식//extends Apple상속관계
    void func02() {//class 자식 extends 부모
        System.out.println("코끼리");
    }
    void func03() {
        System.out.println("자식 독수리");
    }
    void func04() {
        //func02();
        //func03();//자식 독수리가 출력이된다.
        super.func03();//super는 부모를 나타낸다.
        //이방법은 간접적으로 부모를 호출하는 방법이다.
    }
    void func05() {
        System.out.println("1");
        func06();
    }
    void func06() {
        System.out.println("2");
    }
}
//원래 만들어진 class = 부모
//
public class Hello {
    public static void main(String[] args) {
    /*Apple a1 = new Apple();    
    a1.func01();*/
    Orange o1 = new Orange();
    o1.func02();
    o1.func01();//부모 클래스에 있는 func01()을 출력할 수 있다.
    o1.func03();//둘다 함수가 있을 경우에는 자신의 것을 먼저 쓴다.
    //상속에서 부모의 함수이름과 자식의 함수이름이 같은것을
    //함수 오버라이딩이라고한다.
    
    //오버로딩은 같은 클래스의 함수 인수의 갯수가 다르던지, 타입이 다를때
    //이름이 같아도 쓸 수 있다.
    
    o1.func03();//오버라이딩 되었을때 부모의 함수 3번을 호출할 방법은 없다.
    o1.func04();
    o1.func05();
    o1.func06();
    
    
    }
}
cs




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
32
33
34
35
36
37
38
39
40
41
42
43
package Pack01;
import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
class Apple{//부모
    void func01() {
        System.out.println("1");
        func02();
    }
    void func02() {
        System.out.println("2");
    }
}
class Orange extends Apple{//자식//extends Apple상속관계
    void func02() {
        System.out.println("3");
    }
}
class Kiwi extends Orange{
    //Apple할아버지 orange 아빠 kiwi 아들
    void func03() {
        System.out.println("4");
    }
}
//원래 만들어진 class = 부모
//
public class Hello {
    public static void main(String[] args) {
        Orange o1 = new Orange();
        o1.func01();//함수 1번 콜에서도 자식걸 호출한다.
        //super을 쓰지않는 한 무조건 자식것을 호출?
        o1.func02();
        //o1.func03();//부모는 자식함수를 쓸 수 없다. 쓰려면 오버라이딩 해야한다.
        Kiwi k1 = new Kiwi();
        System.out.println("-----");
        k1.func01();//13
        k1.func02();//3
        k1.func03();//4
    }
}
cs




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
package Pack01;
import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
class Animal{
    String n;
    Animal(){
        
    }
    Animal(String n){
        this.n = n;//함수의 인수 n을 필드에 넣겠다~
    }
    void run() {
        System.out.println(n+"가 달립니다.");
    }
}
public class Hello {
    public static void main(String[] args) {
        Animal a1 = new Animal("호랑이");
        a1.run();
        Animal a2 = new Animal("코끼리");
        a2.run();
    }
}
cs



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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package Pack01;
import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
class Animal{
    String n;
    Animal(){
        
    }
    void run() {
        System.out.println(n+"가 달립니다.");
    }
    void sound() {
        cry();
    }
    void cry() {
        System.out.println("저는 울 수 없어요");
    }
}
class Dog extends Animal{
    Dog(String n){
        super.n = n;
    }
    void cry(){
        System.out.println("멍멍");
    }
}
class Cat extends Animal{
    Cat(String n){
        super.n = n;
    }
    void cry(){
        System.out.println("야옹");
    }
}
class Snake extends Animal{
    Snake(String n){
        super.n = n;
    }
    void run() {
        System.out.println(super.n+"은 기어갑니다.");
    }
}
public class Hello {
    public static void main(String[] args) {
        Dog dog = new Dog("강아지");
        dog.run();
        Cat cat = new Cat("나비");
        cat.run();
        Snake snake = new Snake("뱀");
        snake.run();
        System.out.println();
        dog.sound();
        cat.sound();
        snake.sound();
    }
}
cs




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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package Pack01;
import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.SynchronousQueue;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
class 도형 {
    void func01() {
        System.out.print("면적은 ");
    }
    void func02() {
        System.out.print("입니다.");
    }
}
class 삼각형 extends 도형{
    void func03(int w,int h) {
        func01();
        System.out.print(w*h/2);
        func02();
    }
}
class 사각형 extends 도형{
    void func03(int w,int h) {
        func01();
        System.out.print(w*h);
        func02();
    }
}
class 원 extends 도형{
    void func03(int r) {
        func01();
        System.out.print(r*r*3.14);
        func02();
    }
}
public class Hello {
    public static void main(String[] args) {
        //상속구조가 맞으면 되는거다. ex 고양이는 동물이다!
        원 c= new 원();
        삼각형 t = new 삼각형();
        사각형 r = new 사각형();
        c.func03(3);
        System.out.println();
        t.func03(42);
        System.out.println();
        r.func03(42);
    }
}
cs


728x90

'비트 단기 > java' 카테고리의 다른 글

다형성 이어서 추상클래스  (0) 2018.12.06
java 다형성  (0) 2018.12.05
java 41번  (0) 2018.12.03
24~ 31  (0) 2018.11.29
java class 2 17~24  (0) 2018.11.28

댓글