My question is about an ArrayStoreException I’m getting. It is at line 45 of YaSort class I’ve pasted below, at line:
result[m] = (Object) input[m];
Apparently, I’m trying to assign incompatible types, but I don’t see how that is happening.
Here is my code:
(Sorry, stackoverflow does not allow me to post more than two links, so I’ll have to copy-paste the code here.
Class DVD, the one to be compared:
// 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());
}
}
Class YaSort, which includes two sorting algorithms. I’m using the second one, insertionSort:
// 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;
}
}
Class DVDCollection, which represents a DVD collection (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;
}
}
Class Movies, which is there to test if everything else is working:
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);
}
}
By the way, I’m aware that using arrays when the size of the data is expected to change doesn’t make a lot of sense, but the book I’m trying to study gave this example like this. I also another version with ArrayLists, but it has different problems.
Thank you for helping!
In java arrays are classes too. Therefore calling .getClass() on an array will return the array class and not the class of the elements contained in the array.
Use .getClass().getComponentType() to determine the contained class and use that to create an array through newInstance. Or create an exact copy with Arrays.copyOf().