java - java.lang.ArrayStoreException
问题描述
当我使用 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.
公共类 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);
推荐阅读
- javascript - 将 Python 字符串列表导入 Javascript 脚本(HTML 文件)
- python - 有没有更好的方法来覆盖 Marshmallow.fields.List?
- redirect - 登录站点 intead Admin/cmsadministration.aspx 后如何重定向到 kentico 的主页?
- javascript - JavaScript:以功能方式重新组织画布渲染
- java - 映射和数组列表,如果索引为 getEntry() 为 null,则添加“0”
- sql - 如何在 BIG QUERY 中仅选择增量记录
- .net-core - JWT 令牌在 dotnet core 2.1 中返回失败
- angular - Firebase / Recaptcha / Ionic / Angular - 无法分配给对象'[object Object]'的只读属性'taskData'
- sql-server - 如何过滤嵌套查询中的记录,然后从过滤结果中选择同一表中的其他列?
- android - 如何为 RTL 支持应用程序提供 topLeftRadius?