Reputation: 35
I am supposed to read an input of 4 coordinates Example: 0 1 1 1 (where 0 and 1 are the X and Y coordinates of the 1st point and 1 and 1 are the coordinates of the second point) How am I supposed to store these integers in an arraylist of type Point. Following is my approach :
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();
}
Upvotes: 2
Views: 7236
Reputation: 90
As AppWork Suggested , use java.util.HashMap
to store 4 coordinates. You can sort the map using Comparator if you need. Using java.util.ArrayList
you cannot ensure tracking the coordinates latter.
Ther is no easy way to sort HashMap. An alternative can be to get the keys set and sort it.
Set<Integer> keySet =pointMap.keySet();
List <Integer> keyList=new ArrayList<Integer>(keySet);
Collections.sort(keyList);
Now you can iterate the HashMap using this keyList. Since the keys are integer, No need to use Comoparator. If the keys were String, a Comparator could be used instead.
Upvotes: 2
Reputation: 22039
You can create a custom Object TwoPoint
that holds all 4 coordinates.
Use java.awt.Point
.
I will suggest you to use a HashMap<TwoPoint>
where the key will be the index of your input and value will be TwoPoint
.
For example , if you want to retrieve 4th input , just call 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 + ")";
}
}
}
Sample output
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])
Upvotes: 3
Reputation: 1922
You can't just cast int
to Point
like that.
If your Point
class has a constructor Point(int x, int y)
, then you can read both coordinates from the Scanner
and create Point
from them:
int x = sc.nextInt();
int y = sc.nextInt();
Point coordinates = new Point(x, y);
You will need to modify your loop accordingly, so that you read the correct number of int
s from the input.
Upvotes: 1