• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    미해결

문법질문입니다.

23.02.07 21:03 작성 조회수 269

0

import java.nio.channels.Pipe;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

class Point implements Comparable<Point>{
    public int x, y;
    Point(int x, int y){
        this.x = x;
        this.y = y;
    }
    @Override
    public int compareTo(Point o){
        if(this.x == o.x) return this.y-o.y;
        else return this.x-o.x;
    }
}
class Main{
    public static void main(String[] args){
        Scanner kb = new Scanner(System.in);
        int n = kb.nextInt();
        ArrayList<Point> arr = new ArrayList<>();
        for(int i = 0; i < n; i++){
            int x= kb.nextInt();
            int y= kb.nextInt();
            System.out.println(x+","+y);
            arr.add(new Point(x, y));
        }
        Collections.sort(arr);
        System.out.println(">>");
        for (Point o: arr) System.out.println(o.x+" "+o.y);
    }
}
arr.add(new Point(x, y));

이 부분을 출력해서 값을 프린트하고 싶은데 어떻게 코드를 짜야하나요??

public static void main(String[] args){
    Scanner kb = new Scanner(System.in);
    int n = kb.nextInt();
    ArrayList<Point> arr = new ArrayList<>();
    for(int i = 0; i < n; i++){
        int res;
        int x= kb.nextInt();
        int y= kb.nextInt();
        System.out.println(x+","+y);
        arr.add(new Point(x, y));
        res = arr.add(new Point(x, y));
        System.out.println(res);
    }

이렇게 res 변수를 만들어서 작석했는데 에러가 나네요 ㅠ 도와주세요ㅠㅠㅠ

답변 1

답변을 작성해보세요.

0

원구님의 프로필

원구

2023.02.08

add의 반환타입은 boolean으로 하셔서 그런거 같은데요... 근데 그것을 int타입으로 받아서 그런거 같습니다.

add한 값을 출력을 해주시고 싶으시면 PotinClass에서 ToString메서드를 오버라이딩을 하셔서 구현하시는게 좋을 꺼 같습니다

리쿤님의 프로필

리쿤

질문자

2023.02.08

친절한 답변 감사합니다ㅎㅎ 나중에 교수님도 보시면 답변해주시면 감사하겠습니다 ㅎ