无法编写正确的算法

时间:2012-11-28 05:42:26

标签: java algorithm loops arraylist

我正在尝试编写一个自定义函数来遍历数组列表并比较四边形的四边。每一方只需要比较1比1,但我似乎无法弄清楚循环它们的正确方法。我绘制了一个我附加的图表,但我似乎无法实现正确的循环来做我想做的事情。我制作了一个简化版本的Bounds类,仅用于测试,它使用getSide(1-4)方法获得边1:北,2:东,3:南,4:西。现在我只想打印边进行测试。即(1 - > 11,1 - > 11,2 - > 8,2 - > 12 ...等。)

编辑:我忘了提到最小的数字是我试图让我的算法通过的顺序。也许这棵树并不像我希望的那样清晰,这些线条基本上是比较四边形的四边形到它的相反。所以第一个序列是:矩形1:1 - > 7,然后1 - > 11,然后继续前进到东边,然后将其与西边进行比较,依此类推,直到它穿过所有边。

    /* Program I'm using to test my algorithm */
    public static void test()
    {
       BoundTest a,b,c,d,e,f;
       a = new BoundTest("Rectangle 1","A","B","C","D");
       b = new BoundTest("Rectangle 2","E","F","G","H");
       c = new BoundTest("Rectangle 3","I","J","K","L");

       ArrayList<BoundTest> x = new ArrayList();
       x.add(0,a);
       x.add(1,b);
       x.add(2,c);

       System.out.println(x.size());
       for(int i = 0; i <= (x.size() - 2); i++)
       {
          System.out.println(x.get(i).getName());
       }
    }


    /* Bounding Class */
    public class BoundTest 
    {
       private String north,east,south,west,name;

       public BoundTest(String name,String a,String b,String c,String d)
       { 
          this.name = "Name:" + name;
          this.north = "North:" + a;
          this.east = "East: " + b;
          this.south = "South:" + c;
          this.west = "West: " + d;
       }

       /* Get side */
       public String getSide(int a)
       {
          String returnName;

          switch(a)
          {
            case 1:
              returnName = this.north;
               break;
            case 2: 
               returnName = this.east;
               break;
            case 3: 
               returnName = this.south;
                break;
            case 4: 
               returnName = this.west;
                break;
           default:
               returnName = this.north;
               break;
         }
         return returnName;
    }
 }

enter image description here

0 个答案:

没有答案