gpt4 book ai didi

java.lang.ArrayStoreException

转载 作者:行者123 更新时间:2023-12-01 18:43:51 25 4
gpt4 key购买 nike

当我使用 Junit 进行测试时,我似乎无法运行此测试。它仅在失败跟踪上显示错误和“java.lang.ArrayStoreException”。如果有人能解决我的问题,那将是一个很大的帮助。 LinkedInUser是我制作的一个对象,getConnections方法返回 List<LinkedInUser>数据类型。

@Test
public void testSort() throws LinkedInException {//Test sorting
LinkedInUser user0 = new LinkedInUser("Han", null);
LinkedInUser user1 = new LinkedInUser("LUKE", null);
LinkedInUser user2 = new LinkedInUser("leia", null);

user0.addConnection(user1);//Han gets 2 connections
user0.addConnection(user2);

List<LinkedInUser> friends = user0.getConnections();//Transfer to array
Collections.sort(friends);//Sort

Assert.assertEquals(user2, friends.get(0));//Compare
Assert.assertEquals(user1, friends.get(1));
}

最佳答案

您似乎正在将 LinkedInUser 保存在不匹配的类型数组中。

Java 中的 ArrayStoreException

ArrayStoreException in Java occurs whenever an attempt is made to store the wrong type of object into an array of objects. The ArrayStoreException is a class which extends RuntimeException, which means that it is an exception thrown at the runtime.

公共(public)类ArrayStoreException扩展了RuntimeException

Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects. For example, the following code generates an ArrayStoreException:
Object x[] = new String[3];
x[0] = new Integer(0);

关于java.lang.ArrayStoreException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59874429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com