對於我的計算機類科學類中的最後一個項目,我需要創建一個具有2個類的項目,並讀取帶有矩形(x,y,寬度,高度)座標的CSV一個數組列表並打印距離它們的拐角(x,y)最小距離的2個矩形我已經設法打印出最小的矩形,但我現在不知道如何打印第二小矩形。來自一個arraylist的最小的2個值
這裏是我的代碼
public class Week09 {
public static void main(String[] args)throws IOException {
String theFile;
theFile = getTheFileName();
ArrayList<Rectangle> arrayRectangle;
arrayRectangle = getArraylist(theFile);
displaySameArea(arrayRectangle,"Rectangles with same area: ");
displaysmallDist(arrayRectangle,"Recangles with smallest distance: ");
}
public static String getTheFileName(){
String theFile;
JFileChooser jfc = new JFileChooser();
jfc.showOpenDialog(null);
return theFile = jfc.getSelectedFile().getAbsolutePath();
}
public static ArrayList<Rectangle> getArraylist(String s) throws IOException {
ArrayList <Rectangle> arrayRectangle = new ArrayList <Rectangle>();
FileReader fr = new FileReader(s);
BufferedReader br = new BufferedReader(fr);
int x = 0;
int y = 0;
int width = 0;
int height = 0;
String aLine;
String arrayLine = "[,]";
try{
while ((aLine=br.readLine()) != null){
String[] a = aLine.split(arrayLine);
x = Integer.parseInt(a[0]);
y = Integer.parseInt(a[1]);
width = Integer.parseInt(a[2]);
height = Integer.parseInt(a[3]);
Rectangle b = new Rectangle(x,y,width,height);
arrayRectangle.add(b);
}
}
catch (IOException e) {
e.printStackTrace();
}
return arrayRectangle;
}
public static void displaysmallDist(ArrayList<Rectangle> arrayRectangle, String s) {
int x = 0;
int y = 0;
int width = 0;
int height = 0;
int count1 = 0;
int count2 = 1;
Rectangle c = new Rectangle (x,y,width,height);
Rectangle d = new Rectangle (x,y,width,height);
double lowestDistance = Double.MAX_VALUE;
for(int n = 0; n < arrayRectangle.size(); n++) {
c = arrayRectangle.get(n);
for(int j = n+1; j < arrayRectangle.size(); j++) {
double nextDistance;
d = arrayRectangle.get(j);
nextDistance = c.distance(d);
if (nextDistance < lowestDistance)
{
lowestDistance = nextDistance;
count1 = n;
count2 = j;
}
}
}
System.out.print(s + arrayRectangle.get(count1).toString() + arrayRectangle.get(count2).toString() + "\n");
}
}
這裏是我的第二類
public class Rectangle {
private int x;
private int y;
private int width;
private int height;
public Rectangle(int x,int y,int width,int height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public Rectangle(Rectangle a) {
x = a.x;
y = a.y;
width = a.width;
height = a.height;
}
public void setX (int x){
this.x = x;
}
public int getX(){
return x;
}
public void setY (int y){
this.y = y;
}
public int getY(){
return y;
}
public void setWidth (int width){
this.width = width;
}
public int getWidth(){
return width;
}
public void setHeight (int height){
this.height = height;
}
public int getHeight(){
return height;
}
public double getArea(){
return this.width * this.height;
}
public double distance(Rectangle y){
return Math.sqrt((this.x - y.x)*(this.x - y.x) + (this.y - y.y)*(this.y - y.y));
}
public boolean equals(Rectangle a){
if (this.width * this.height != a.width * a.height) return false;
return true;
}
public String toString(){
return "Rectangle corner at ("+ x +"," + y + ") Width = " + width + " Height = " + height + " ";
}
}
是否有可能使/實現您的Rectangle類可比較和使用自動比較和排序對象的TreeSet?看看http://www.programcreek.com/2009/02/implement-comparable-for-a-treeset/ –
爲了澄清,你想要打印的數組中的兩個矩形的起點(x,y)是彼此最近?或者誰有最接近的角落? – Jason
(x,y)是左上角的座標,它是CSV(x,y,寬度,高度)中的x和y。 –