2017-02-10 136 views
0

因此,給定一個數組的元素,我試圖創建一個位置對象的數組列表,它包含從給定位置(對角線和旁邊的,但不包括給定位置)相鄰的所有元素。 這裏是我的位置類:如何從另一個元素創建相鄰元素的數組列表?

public class Location 
{ 
// Row and column positions. 
private int row; 
private int col; 


public Location(int row, int col) 
{ 
    this.row = row; 
    this.col = col; 

} 
//getRow() & getCol() methods not shown 

這裏是我試圖在另一個類來完成方法:

public List<Location> adjacentLocations(Location location) 
{ 
    List<Location> locations = new ArrayList<>(); 
    //todo, diag, next 

    return locations 
} 
+0

提供幾個示例輸入以及您對每個樣本輸入的預期輸出。 –

回答

0

爲什麼不直接指定列表作爲一個ArrayList開始。 例如:

ArrayList<Location> locations = new ArrayList<>(); 

public ArrayList<Location> adjacentLocations(Location location) 
{ 
    locations.add(location); 
} 

public ArrayList<Location> getLocations() 
{ 
    for(int i = 0; i < locations.length; i++) 
    { 
     System.out.println(locations[i]); 
    } 
} 
0

我假設它是無效的具有負座標。

public List<Location> adjacentLocations(Location location) 
{ 
    List<Location> locations = new ArrayList<Location>(); 

    // Calculate all the adjacent positions relative to the specified point. 
    int row = location.getRow(); 
    int col = location.getCol(); 
    for (int r = row -1; r<row +2; r++) 
    { 
     for (int c = col -1; c < col + 2; c++) 
     { 
      if ((c > -1 && r > -1) && !(r == row && c == col)) 
      { 
       locations.add(new Location(r, c)); 
      } 
     } 
    } 

    return locations; 
} 

填妥類來說明用法:

package stacktest; 

import java.util.ArrayList; 
import java.util.List; 

public class Location { 

    // Row and column positions. 
    private int row; 
    private int col; 

    public Location(int row, int col) 
    { 
     this.row = row; 
     this.col = col; 

    } 

    public int getRow() 
    { 
     return this.row; 
    } 

    public int getCol() 
    { 
     return this.col; 
    } 

    public String toString() 
    { 
     return "[" + row + ", " + col + "]"; 
    } 

    public List<Location> adjacentLocations() 
    { 
     List<Location> locations = new ArrayList<Location>(); 

     // Calculate all the adjacent positions relative to the specified point. 
     for (int r = row -1; r<row +2; r++) 
     { 
      for (int c = col -1; c < col + 2; c++) 
      { 
       if ((c > -1 && r > -1) && !(r == row && c == col)) 
       { 
        locations.add(new Location(r, c)); 
       } 
      } 
     } 

     return locations; 
    } 

    public static void main(String[] args) 
    { 
     ArrayList<Location> originalLocations = new ArrayList<Location>(); 
     originalLocations.add(new Location(4, 10)); 
     originalLocations.add(new Location(100, 100)); 
     originalLocations.add(new Location(1, 0)); 

     for (Location l: originalLocations) 
     { 
      List<Location> adjacent = l.adjacentLocations(); 
      System.out.println("orig: " + l); 
      for (Location adj: adjacent) 
      { 
       System.out.println(" -> " + adj); 
      } 
     } 
    } 
} 

和輸出:

orig: [4, 10] 
-> [3, 9] 
-> [3, 10] 
-> [3, 11] 
-> [4, 9] 
-> [4, 11] 
-> [5, 9] 
-> [5, 10] 
-> [5, 11] 
orig: [100, 100] 
-> [99, 99] 
-> [99, 100] 
-> [99, 101] 
-> [100, 99] 
-> [100, 101] 
-> [101, 99] 
-> [101, 100] 
-> [101, 101] 
orig: [1, 0] 
-> [0, 0] 
-> [0, 1] 
-> [1, 1] 
-> [2, 0] 
-> [2, 1] 
+0

謝謝!但爲什麼數組列表最後有兩個空對象? –

+0

@RidgeHaven我已經添加了完整的代碼來在上下文中運行該方法,並且列表中沒有空值,因此您可能在某處出現錯誤。 –

0

,如果我理解正確的,你需要做的所有位置元素location[x][y]的列表給出location[i][j]其中(x,y)指示集對代表的位置屬於集合
{(i-1,j-1),(i+1,j+1),(i,j+1),(i+1,j),(i-1,j),(i,j-1),(i+1,j-1),(i-1,j+1)}

相關問題