English 中文(简体)
原始列表似乎不匹配, 即使值/ 顺序相同? 是否可能?
原标题:primitive list s don t seem to match even when values/order are the same? Is it possible?
  • 时间:2012-05-26 03:31:21
  •  标签:
  • java
  • arrays

我有一份我一次列出一个的插图列表。 我找到了一个结果似乎会反转的隐性模式, 以测试它。 我颠倒了出现在我方法中的列表, 然后展示了它们, 发现它们匹配 。 我想在程序上找到它们, 但是当我打印列表本身为字符串时, 其结果即使项目/ 命令匹配, 也与它不匹配 。

s 这里的方法( 它只是获取数据, 颠倒顺序并打印两个... nothing fancy) :

private static void show (int [] arr) { 
    //before we print results lets reverse the list and study if there s something we can do to make this go faster
    int[] ReversedList = new int[arr.length];
    for (int x = arr.length-1, y=0; x>=0;x--, y++) {
        ReversedList[y] = arr[x];
    }
    for (int x = 0; x < arr.length; x++) {
        //System.out.print (" " + (arr [x] + low[x]));
        System.out.print (" " + (arr[x]));
    }
    System.out.println (" " + arr.toString());
    for (int x = 0; x < ReversedList.length; x++) {
        //System.out.print (" " + (arr [x] + low[x]));
        System.out.print (" " + (ReversedList[x]));
    }

    System.out.println (" " + ReversedList.toString() + "  ***");
   //System.out.println("*****************");
}

但字符串不匹配。 这里显示输出的片段( 表示它被反转 ) :

 0 0 0 20 [I@199a0c7c
 20 0 0 0 [I@50a9ae05  ***
....
 20 0 0 0 [I@1e9af0b1
 0 0 0 20 [I@4e300429  ***

它们都像这样,我不确定为什么它们不能在相同的时候匹配(至少看起来是一样的)。我有一个名为 结果 的阵列列表,该列表拥有全部的内涵,我试图做一个结果。索引(refectd)但没有运气(I get -1 for everything)。我如何找到匹配?

最佳回答

您正在查看您拥有的插数组的地址。 数据是相同的, 但数据存储在多个位置, 因为您正在使用多个实例 。

为了比较阵列,请使用 < a href="http://docs.oracle.com/javase/6/docs/api/java/ util/ Arrays.html#qualps%28int%5B%5D.%20int%5B%29" rel="nofollow"\\code>Arrays.epares(int) 。或者,您可以用 < a href="http://docs.oracle.com/javase/6/docs/api/java/ util/Arrays.html#toString%28int%5B%29" rel="nofolcool>Arrays.toString(int) 来查看阵列中的信息。

问题回答

这些是所有对象的地址。 它们指不同的整数数组, 这样它们就不会匹配 。

使用 < a href=" "http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString%28int%5b%5d%29" rel="nofollow"\\code>java.util.Arrays.to String(int) 方法,而不是直观地看到一个阵列的内容:

import java.util.Arrays;
// ... code
System.out.println (" " + Arrays.toString(arr));




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...