简体   繁体   English

如何正确比较 JUnit 中的两个链表?

[英]How to compare two Linked Lists correctly in JUnit?

I have the following sentence:我有以下句子:

assertArrayEquals(Arrays.asList(df.sort("State", new SortDescending())).toArray(),Arrays.asList("[WV, WV, WI, WA, VA, TX, SD, PA, OH, ND, NC, NC, MB, MA, KS, FL, DE]").toArray());

The return of the function is a List< Object > and it contains: function 的返回是一个 List< Object > 它包含:

[WV, WV, WI, WA, VA, TX, SD, PA, OH, ND, NC, NC, MB, MA, KS, FL, DE]

The error is:错误是:

Expected :java.util.LinkedList<[WV, WV, WI, WA, VA, TX, SD, PA, OH, ND, NC, NC, MB, MA, KS, FL, DE]>
Actual   :java.lang.String<[WV, WV, WI, WA, VA, TX, SD, PA, OH, ND, NC, NC, MB, MA, KS, FL, DE]>

Why i don't pass the test when I compare these two lists?当我比较这两个列表时,为什么我没有通过测试?

In Java, " characters specify a String value. Not a List. Just a single String value.在 Java 中, "字符指定一个字符串值。不是列表。只是一个字符串值。

This:这个:

"[WV, WV, WI, WA, VA, TX, SD, PA, OH, ND, NC, NC, MB, MA, KS, FL, DE]"

is not a List.不是列表。 It's a single String object, whose value may happen to look like a List, but it's not a List.它是一个单一的字符串 object,其值可能恰好看起来像一个列表,但它不是一个列表。 It is in double-quotes ( " ), therefore it is a single String object.它位于双引号 ( " ) 中,因此它是单个字符串 object。

This:这个:

Arrays.asList("[WV, WV, WI, WA, VA, TX, SD, PA, OH, ND, NC, NC, MB, MA, KS, FL, DE]")

create a List which contains one element.创建一个包含一个元素的列表。 It is a List<String> with a size of one.它是一个大小为 1 的List<String>

Arrays.asList does not attempt to parse Strings in any way. Arrays.asList不会尝试以任何方式解析字符串。 You cannot specify a List of items inside a single String value.您不能在单个字符串值内指定项目列表。

What you probably meant to write was this:你可能打算写的是这样的:

Arrays.asList("WV", "WV", "WI", "WA", "VA", "TX", "SD", "PA", "OH", "ND", "NC", "NC", "MB", "MA", "KS", "FL", "DE")

Notice that are no [ or ] characters.请注意,它们不是[]字符。 The List is comprised of individual String values, each specified as a separate argument to Arrays.asList.该列表由单独的字符串值组成,每个字符串值都指定为 Arrays.asList 的单独参数。

You might find it easier to just compare the Lists directly:您可能会发现直接比较列表更容易:

Assert.assertEquals("Checking for correctly sorted list.",
    Arrays.asList("WV", "WV", "WI", "WA", "VA", "TX", "SD", "PA", "OH", "ND", "NC", "NC", "MB", "MA", "KS", "FL", "DE"),
    Arrays.asList(df.sort("State", new SortDescending())));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM