我的问题是关于我得到的ArrayStoreException。它位于YaSort类的第45行,我在下面粘贴了一行:
result[m] = (Object) input[m];
显然,我正在尝试分配不兼容的类型,但我不知道这是怎么回事。
这是我的代码:
(抱歉,stackoverflow不允许我发布两个以上的链接,所以我必须在这里复制粘贴代码。
类DVD,要比较的一个:
// A single DVD.
import java.text.NumberFormat;
public class DVD implements Comparable {
private String title, director;
private int year;
private double cost;
private boolean bluray;
public DVD(String title, String director, int year, double cost,
boolean bluray) {
this.title = title;
this.director = director;
this.year = year;
this.cost = cost;
this.bluray = bluray;
}
public String toString() {
NumberFormat myFormat = NumberFormat.getCurrencyInstance();
String description = myFormat.format(cost) + "\t" + year + "\t" +
title + "\t" + director;
if (bluray)
description += "\t" + "Blu-ray";
return description;
}
public String getTitle() {
return title;
}
public int compareTo(Object input) {
return title.compareTo(((DVD)input).getTitle());
}
public boolean equals(Object input) {
return title.equals(((DVD)input).getTitle());
}
}
类YaSort,包括两个排序算法。我正在使用第二个,insertSort:
// implements various sorting algorithms for the Comparable interface
import java.lang.reflect.Array;
public class YaSort {
public static Comparable[] selectionSort(Comparable[] input) {
int largestOne;
Comparable temp;
Comparable[] result;
result = input.clone();
for (int k = 0; k < result.length - 1; k++) {
largestOne = k;
for (int j = k + 1; j < result.length; j++) {
if (result[largestOne].compareTo(result[j]) < 0) {
largestOne = j;
}
}
temp = result[k];
result[k] = result[largestOne];
result[largestOne] = temp;
}
return result;
}
public static Comparable[] insertionSort(Comparable[] input) {
// don't forget to remove empty references in the input
Object temp;
Object[] result;
int nonEmptyInput = 0;
for (int i = 0; i < input.length; i++) {
if (input[i] != null)
nonEmptyInput++;
}
result = (Object[]) Array.newInstance(input.getClass(), nonEmptyInput);
for (int m = 0; m < nonEmptyInput; m++)
result[m] = (Object) input[m];
if (result.length > 1) {
for (int k = 1; k < result.length; k++) {
for (int j = 1; j <= k; j++) {
if (((Comparable)result[k - j]).compareTo(result[k - j + 1]) < 0) {
temp = ((Comparable)result[k - j + 1]);
result[k - j + 1] = result[k - j];
result[k - j] = temp;
}
}
}
}
return (Comparable[]) result;
}
}
类DVDCollection,代表DVD集合(doh!):
// Represents a collection of DVD movies.
import java.text.NumberFormat;
public class DVDCollection {
private final int INITIAL_SIZE = 100;
private DVD[] members;
private int count; // really?
private double totalCost;
public DVDCollection() {
members = new DVD[INITIAL_SIZE];
count = 0;
totalCost = 0.0;
}
public void addDVD(String title, String director, int year, double cost,
boolean bluray) {
if(count == members.length)
increaseSize();
members[count] = new DVD(title, director, year, cost, bluray);
totalCost += cost;
count++;
members = (DVD[]) YaSort.insertionSort(members);
// Object members2 = new Object[members.length];
// members2 = YaSort.insertionSort(members);
// for (int k = 0; k < members.length; k++) {
// System.out.println(members2[k].getClass());
// }
}
private void increaseSize() {
DVD[] temp = new DVD[members.length * 2];
for (int k = 0; k < members.length; k++)
temp[k] = members[k];
members = temp;
}
public String toString() {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String report = "*******************************************\n";
report += "My DVD Collection\n\n";
report += "Number of DVDs: " + count + "\n";
report += "Total cost: " + fmt.format(totalCost) + "\n";
report += "Average cost: " + fmt.format(totalCost / count);
report += "\n\nDVD List:\n\n";
for (int nDvd = 0; nDvd < count; nDvd++)
report += members[nDvd].toString() + "\n";
return report;
}
}
班级电影,可以测试其他一切是否正常工作:
public class Movies {
public static void main(String[] args) {
DVDCollection movies = new DVDCollection();
movies.addDVD("The Godfather", "Francis Ford Coppola", 1972, 24.95, true);
movies.addDVD("District 9", "Neill Blokamp", 2009, 19.95, false);
movies.addDVD("Iron Man", "Jon Favreau", 2008, 15.95, false);
movies.addDVD("All About Eve", "Joseph Mankiewicz", 1950, 17.50, false);
movies.addDVD("The Matrix", "Andy & Lana Wachowski", 1999, 19.95, true);
System.out.println(movies);
movies.addDVD("Iron Man 2", "Jon Favreau", 2010, 22.99, false);
movies.addDVD("Casablanca", "Michael Curtiz", 1942, 19.95, false);
System.out.println(movies);
}
}
顺便说一句,我知道在预期数据大小发生变化时使用数组并没有多大意义,但我正在尝试研究的这本书给出了这样的例子。我也是ArrayLists的另一个版本,但它有不同的问题。
感谢您的帮助!
答案 0 :(得分:1)
在java数组中也是类。因此,在数组上调用.getClass()将返回数组类,而不是数组中包含的元素的类。
使用.getClass().getComponentType()确定包含的类,并使用它通过newInstance创建一个数组。或使用Arrays.copyOf()创建精确副本。
答案 1 :(得分:0)
问题在于这一行:
result = (Object[]) Array.newInstance(input.getClass(), nonEmptyInput);
输入的类型为Comparible[]
,但我认为您打算放入Comparible
类型。为此,请尝试:
result = (Object[]) Array.newInstance(Comparable.class, nonEmptyInput);