怎样用Java编写代码,随机生成这样一组不重复的坐标:
(0,0)(0,1)(0,2)
(1,0)(1,1)(1,2)
(2,0)(2,1)(2,2)
各位高手请赐教!

解决方案 »

  1.   

    先把你的这些坐标按顺序生成,然后放到List中,然后使用;Collections.shuffle(list),就可以生成随机的坐标了。
      

  2.   

    用Point类(自己定义也可以.)重写equals方法,和HashCode()这样才能保证不重复.关于不重复,可以用Set,也可以自己编程写一个方法.
    随机就用1楼的办法.
      

  3.   

    首先定义坐标,大概像这样子public class Point {

    private int x;
    private int y;

    }然后定义一个类来随即生成不重复坐标public class PointGenerator {
    Set<Point> generatedPoint;//记录已经生成的point

    static void generateNextPoint(){
    //用java.util.Random中的 nextInt(int n) //n 为你的坐标允许的最大值
    //生成一个一个point
    // while (point 在GeneratedPoint中){
          //生成下一个随机point
          //把point 放入 generatedPoint
         //break;
    }
    }
    }
    当然必须重写 equal 和 hashcode,这样才能正确判断一个点是不是在generatedPoint中
      

  4.   

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Random;public class Rd {
    static Map setRd(int n) {
    Random out = new Random(System.currentTimeMillis());
    Map contain = new HashMap();
    while (n > 0) {
    int x = out.nextInt(10);
    int y = out.nextInt(10);
    if (!contain.containsValue("(" + x + "," + y + ")"))
    contain.put(n--, "(" + x + "," + y + ")");
    } return contain;
    } public static void main(String[] args) throws IOException {
    System.out.println("enter the numbers of your coordinate:");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String s = br.readLine();
    int c = Integer.parseInt(s);
    Map result = Rd.setRd(c);
    for (int a = 1; a < result.size() + 1; a++) {
    System.out.print(" " + result.get(a));
    }
    }
    }