我的問題是關於我得到的ArrayStoreException。正是在YaSort類的45線,我粘貼下面,在行:ArrayStoreException
result[m] = (Object) input[m];
很顯然,我試圖分配不兼容的類型,但我不明白這是如何發生的。
這裏是我的代碼:
(對不起,計算器不允許我發表兩個以上的鏈路,所以我不得不代碼在這裏複製粘貼
類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,其包括兩個排序算法我使用的第二個,插入排序:
// 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);
}
}
順便說一句,我m知道,當數據的大小預計會發生變化時使用數組並沒有什麼意義,但是我試圖研究的這本書給出了這樣的例子。我也是ArrayLists的另一個版本,但它有不同的問題。
謝謝你的幫忙!
而不是做代碼轉儲,建立一個顯示相同問題的* short *程序會更有用。 – Dennis
「問題太長了,請下一位病人。」 –
*「...所以我必須在這裏複製粘貼代碼。」* **不,你不必。**正確的方法是編寫一個SMAll示例程序來說明你的問題......並且不要強迫讀者閱讀數百行代碼,其中大部分代碼可能不相關。 –