数组不太适合您的工作。它们不仅没有像 intersection 或 removeAll 这样的方法,而且很难改变,尤其是如果你想减小它们的大小。
为了避免这种麻烦,我removed
通过将 x 和 y 设置为 -1 来标记一个点,一个坐标当然就足够了,但是出于对称的原因......
import java.util.*;
import java.awt.Point;
public class RandPoints
{
Random r = new Random ();
Point [][] arr;
public void removeDuplicatesFrom2 (Point [] pa, Point [] pb) {
for (Point p : pb) {
if (p.x != - 1 && Arrays.asList (pa).contains (p)) {
// System.out.println ("dup: " + p.x + " " + p.y);
p.x = -1;
p.y = -1;
}
}
}
// create a random point in range (0..19)(0..19)
public Point randpoint () {
return new Point (r.nextInt (20), r.nextInt (20));
}
// create 10 (default) arrays of size 100 (default)
public void init (int arrsize, int arrcount)
{
arr = new Point [arrcount][];
for (int c = 0; c < arrcount; ++c)
{
Point [] points = new Point [arrsize];
for (int s = 0; s < arrsize; ++s)
{
Point p = randpoint ();
points[s] = p;
}
arr[c] = points;
}
}
public void unify ()
{
for (Point[] pac0 : arr) {
for (Point[] pac1 : arr)
{
if (pac0 != pac1)
removeDuplicatesFrom2 (pac0, pac1);
}
}
}
/**
fill arrays with duplicates by random,
show, delete and show again.
*/
public RandPoints (int arrsize, int arrcount)
{
init (arrsize, arrcount);
show ();
unify ();
System.out.println ();
show ();
}
public static void main (String args[])
{
int arrsize = 100;
int arrcount = 10;
if (args.length == 2)
{
arrsize = Integer.parseInt (args[0]);
arrcount = Integer.parseInt (args[1]);
}
new RandPoints (arrsize, arrcount);
}
// visible feedback is always welcome while debugging/testing
public void show ()
{
for (Point[] pa: arr) {
for (Point point: pa)
if (point.x != -1 && point.y != -1)
System.out.print (point.x + ", " + point.y + "\t");
System.out.println ();
}
}
}
我稍微修改了代码,以使值 (20) 的最大范围也可配置。
开始它
java RandPoints 16 10 10
3, 0 9, 0 6, 9 2, 3 6, 9 7, 4 9, 9 2, 5 8, 7 3, 3 9, 5 3, 7 0, 5 7, 6 0, 4 8, 1
6, 1 2, 7 2, 5 6, 7 0, 7 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