从用户输入创建点对象

时间:2015-09-23 18:39:28

标签: java arraylist

我应该读取4个坐标的输入 示例:0 1 1 1(其中0和1是第1点的X和Y坐标,1和1是第2点的坐标) 我应该如何将这些整数存储在Point类型的arraylist中。 以下是我的方法:

  public class Project1 {
     private int m;
     private int n;
     private WeightedQuickUnionUF qu;
     private int[][] grid;
     private ArrayList<Point> connections;

    /**
    * initializes UnionFind structure, grid and connection list
    * @param m
    * @param n
    */
    public Project1(int m, int n){
    m=m;
    n=n;
    for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
     grid = new int[i][j];
    }
   }
 connections = new ArrayList<Point>();
 int size = m*n;
 qu = new WeightedQuickUnionUF(size);

   }

  public void read_input() 
  {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter number of pairs of connections:  ");
    int no_of_connections = sc.nextInt();
    for(int i=0; i < (no_of_connections * 4); i++){
    Point coordinates = (Point)sc.nextInt();
    connections.add(coordinates);
  }
   sc.close();
 } 

3 个答案:

答案 0 :(得分:3)

您可以创建一个包含所有4个坐标的自定义对象TwoPoint。 使用java.awt.Point。 我建议您使用HashMap<TwoPoint>,其中键将是您输入的索引,值将为TwoPoint。 例如,如果要检索第4个输入,只需调用pointMap.get(4)

import java.awt.Point;
import java.util.HashMap;
import java.util.Scanner;

public class StorePoints
{
    static int  i   = 0;

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);

        HashMap<Integer, TwoPoints> pointMap = new HashMap<Integer, StorePoints.TwoPoints>();
        StorePoints sp = new StorePoints();
        for (int i = 0; i < 3; i++)
        {
            System.out.println("Enter 4 coordinates: input ="+(i+1) +"out of  3");
            sp.takeInput(sc,pointMap);
        }

        System.out.println(" Enter index of coordinate  you want to see : [1-3]");
        int index=sc.nextInt();
        if(index<1||index>3)
            System.out.println("Wrong index");
        else
        System.out.println("Input index="+index+", "+pointMap.get(index-1).toString());
        sc.close();
    }

    public void takeInput(Scanner sc,HashMap<Integer, TwoPoints> pointMap)
    {
        int int1=sc.nextInt();int int2= sc.nextInt();int int3= sc.nextInt();int int4= sc.nextInt();
        TwoPoints tp=new TwoPoints(int1,int2,int3,int4);
        pointMap.put(i++, tp);
    }

    class TwoPoints
    {

        Point   p1;
        Point   p2;

        public TwoPoints(int int1, int int2, int int3, int int4)
        {
            p1 = new Point(int1, int2);
            p2 = new Point(int3, int4);
        }

        // getters and setters
        public Point getP1()
        {
            return p1;
        }

        public void setP1(Point p1)
        {
            this.p1 = p1;
        }

        public Point getP2()
        {
            return p2;
        }

        public void setP2(Point p2)
        {
            this.p2 = p2;
        }

        public String toString()
        {
            return "(" + p1 + "," + p2 + ")";
        }
    }
}

示例输出

Enter 4 coordinates: input =1out of  3
1 1 2 3
Enter 4 coordinates: input =2out of  3
2 2 3 4
Enter 4 coordinates: input =3out of  3
2 2 4 5
 Enter coordinate number you want to see : [1-3]
1
Input index=1, (java.awt.Point[x=1,y=1],java.awt.Point[x=2,y=3])

答案 1 :(得分:2)

作为AppWork建议,使用java.util.HashMap存储4个坐标。如果需要,您可以使用Comparator对地图进行排序。使用java.util.ArrayList,您无法确保跟踪后者的坐标。

Ther是排序HashMap的简单方法。另一种方法是获取密钥并对其进行排序。

Set<Integer> keySet =pointMap.keySet();
List <Integer> keyList=new ArrayList<Integer>(keySet);
Collections.sort(keyList);

现在您可以使用此keyList迭代HashMap。由于键是整数,因此无需使用Comoparator。如果键是String,则可以使用Comparator。

答案 2 :(得分:1)

您不能像这样将int投射到Point

如果您的Point类有构造函数Point(int x, int y),那么您可以从Scanner读取这两个坐标并从中创建Point

int x = sc.nextInt();
int y = sc.nextInt();
Point coordinates = new Point(x, y);

您需要相应地修改循环,以便从输入中读取正确数量的int