Skip to content

simple-linked-list: if not generic, attempting to type array yields "Unchecked cast" warnings #229

Closed

Description

@jtigger

Currently, tests for SimpleLinkedList do not explicitly expect the class to be generic.

However, SimpleLinkedList.asArray() takes an instance of Class as a parameter. In order to set values in the constructed around, an unsafe downcast must be made, yielding warnings from the complier.

For example:

public <E> E[] asArray(Class<E> elementType) {
	E[] array = (E[]) Array.newInstance(elementType, size);  // Unchecked cast warning
	for(int idx = 0; idx < array.length; idx++) {
		array[idx] = (E) pop(); // Unchecked cast warning
	}
	return array;
}

Technically, the tests will continue to work if (like the rest of the interface of SimpleLinkedList, plain Object references are handled/returned...

public Object[] asArray() {
	Object[] array = new Object[size];
	for(int idx = 0; idx < array.length; idx++) {
		array[idx] = pop();
	}
	return array;
}

The current test suite forces the practitioner to straddle the two approaches. Let's clear this up!

  1. Remove the type parameter from asArray() and always return an array of Objects.
  2. Rework the test suite to expect SimpleLinkedList to be generic.

@exercism/java Thoughts?

Activity

  1. jtigger commented on Jan 9, 2017

    @jtigger
    ContributorAuthor

    Also see #105.

  2. stale commented on May 6, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions