Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查两个数组是否具有相同的值?_Java_Arrays - Fatal编程技术网

Java 检查两个数组是否具有相同的值?

Java 检查两个数组是否具有相同的值?,java,arrays,Java,Arrays,我想比较两个数组,看看它们是否有相同的值 如果我有一个名为 public static float data[][] 它保存着一个地形的Y坐标,我怎么能用另一个来检查这个阵列呢 public static int coords[][] 没有遍历所有坐标 两个数组中都有1000多个值。遍历它们不是一个选项,因为我必须每秒遍历它们四次 我这样做是为了找出两个物体是否相撞。我已经尝试使用库来实现这一点,但是我找不到像我需要的那样具体的逐坐标碰撞检测 编辑:为什么我不能在这一小片顶点上迭代是这样的。

我想比较两个数组,看看它们是否有相同的值

如果我有一个名为

public static float data[][]
它保存着一个地形的Y坐标,我怎么能用另一个来检查这个阵列呢

public static int coords[][]
没有遍历所有坐标

两个数组中都有1000多个值。遍历它们不是一个选项,因为我必须每秒遍历它们四次

我这样做是为了找出两个物体是否相撞。我已经尝试使用库来实现这一点,但是我找不到像我需要的那样具体的逐坐标碰撞检测

编辑:为什么我不能在这一小片顶点上迭代是这样的。 问题是,这是一个多人游戏,我必须为每个玩家迭代所有1000个坐标。这意味着只有10名玩家在线就有10000名玩家在线就有100名玩家在线就有100000名玩家。您可以看到这将多么容易地延迟,或者至少占用CPU的很大一部分

将坐标输入到“数据”变量中:

try {
        // Load the heightmap-image from its resource file

        BufferedImage heightmapImage = ImageIO.read(new File(
                "res/images/heightmap.bmp"));
        //width = heightmapImage.getWidth();
        //height = heightmapImage.getHeight();
        BufferedImage heightmapColour = ImageIO.read(new File(
                "res/images/colours.bmp"));
        // Initialise the data array, which holds the heights of the
        // heightmap-vertices, with the correct dimensions
        data = new float[heightmapImage.getWidth()][heightmapImage
                .getHeight()];
        // collide = new int[heightmapImage.getWidth()][50][heightmapImage.getHeight()];
        red = new float[heightmapColour.getWidth()][heightmapColour
                .getHeight()];
        blue = new float[heightmapColour.getWidth()][heightmapColour
                .getHeight()];
        green = new float[heightmapColour.getWidth()][heightmapColour
                .getHeight()];
        // Lazily initialise the convenience class for extracting the
        // separate red, green, blue, or alpha channels
        // an int in the default RGB color model and default sRGB
        // colourspace.
        Color colour;
        Color colours;
        // Iterate over the pixels in the image on the x-axis
        for (int z = 0; z < data.length; z++) {
            // Iterate over the pixels in the image on the y-axis
            for (int x = 0; x < data[z].length; x++) {
                colour = new Color(heightmapImage.getRGB(z, x));

                data[z][x] = setHeight;

            }
        }
    }catch (Exception e){
        e.printStackTrace();
        System.exit(1);
    }
试试看{
//从其资源文件加载heightmap图像
BuffereImage heightmapImage=ImageIO.read(新文件(
“res/images/heightmap.bmp”);
//宽度=heightmapImage.getWidth();
//高度=heightmapImage.getHeight();
BuffereImage HeightMapColor=ImageIO.read(新文件(
“res/images/colors.bmp”);
//初始化数据数组,该数组保存
//高度贴图顶点,具有正确的尺寸
数据=新浮点[heightmapImage.getWidth()][heightmapImage
.getHeight();
//collide=newint[heightmapImage.getWidth()][50][heightmapImage.getHeight()];
红色=新浮点[HeightMapColor.getWidth()][HeightMapColor]
.getHeight();
蓝色=新浮动[HeightMapColor.getWidth()][HeightMapColor]
.getHeight();
绿色=新浮动[HeightMapColor.getWidth()][HeightMapColor]
.getHeight();
//惰性地初始化便利类以提取
//单独的红色、绿色、蓝色或alpha通道
//默认RGB颜色模型和默认sRGB中的int
//色彩空间。
颜色;
颜色;
//在x轴上迭代图像中的像素
对于(int z=0;z
以及如何将坐标放入“coords”变量(哦,等等,它被称为“Ship”,而不是coords。我忘了):

试试看{
文件f=新文件(“res/images/coords.txt”);
字符串coords=readTextFile(f.getAbsolutePath());
对于(int i=0;i0)
arr.add(newjavax.vecmath.Vector3f(cor));
如果(!ship.contains(新向量3f((int)sx,(int)sy,(int)sz)))
添加(新矢量3f((int)sx,(int)sy,(int)sz));
//arr.add(新的javax.vecmath.Vector3f(Float.parseFloat(x)、Float.parseFloat(y)、Float.parseFloat(z)); }
谢谢!

恐怕没办法。比较数据集看它们是否相同,需要根据定义查看所有元素。另一方面,即使在相对较旧的硬件上,比较1000个值也不算什么。每秒可以进行数千次。

您可以这样做,但只适用于相同的数据类型

 Arrays.deepEquals(data, coords);
对于一维数组,您可以使用

 Arrays.equals(array1, array1);

试试这个,但这只有在元素有序的情况下才有效。

你试过(并分析过)任何东西吗?你说的“具有相同的值”是什么意思?顺序相同的值?随机顺序相同的值?你知道,1000个值真的很小。你可以在笔记本电脑上每秒迭代数十万次。请输入一些数据[]和坐标[]的示例数据@RakeshSoni我更新了原始帖子。好的。谢谢你的回答。问题是,这是一个多人游戏,我必须为每个玩家迭代所有1000个坐标。这意味着
 Arrays.equals(array1, array1);
Arrays.deepEquals(a, b);