Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9130749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:01:07+00:00 2026-06-17T08:01:07+00:00

My question is about an ArrayStoreException I’m getting. It is at line 45 of

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:01:08+00:00Added an answer on June 17, 2026 at 8:01 am

    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().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

small question about C++ replace function. I'm parsing every line of text input line
Question about subclassing in matlab, under the new class system. I've got class A
A question about inheritance in java... class Base { private int val = 10;
Quick question about JavaScript event objects - how does JavaScript know when I'm trying
Question about controllers. Can controller call it`s own class methods inside an action? EDIT:
Simple question about Jquery-UI sortable lists (http://jqueryui.com/demos/sortable/#default) I have made: <ul id=sortable> <li class=ui-state-default>An
Quick question about jQuery and DOM traversal. Look at the code below and tell
Question about OO design. Suppose I have a base object vehicle. And two descendants:
Question about DOM* class createXXX methods in C++. Do I have to do anything
A question about the flow of information in an object oriented construction, e.g. from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.