-4

我的问题是关于我得到的 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 类,其中包括两种排序算法。我正在使用第二个,插入排序:

// 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 的版本,但它有不同的问题。

谢谢你的帮忙!

4

2 回答 2

1

在 java 中,数组也是类。因此,在数组上调用 .getClass() 将返回数组类,而不是数组中包含的元素的类。

使用.getClass().getComponentType()确定包含的类,并使用它通过 newInstance 创建一个数组。或者使用Arrays.copyOf()创建一个精确的副本。

于 2013-01-12T02:15:28.483 回答
0

问题在于这一行:

result = (Object[]) Array.newInstance(input.getClass(), nonEmptyInput);

输入的类型是 of Comparible[],但我认为您打算输入 type Comparible。为此,请尝试:

result = (Object[]) Array.newInstance(Comparable.class, nonEmptyInput);
于 2013-01-12T02:01:23.170 回答