3

Going back over my basic ADT stuff here, and trying to kill two birds with one stone by learning Java while I amTrying to write a simple algorithm for a merge sort with a generic linked list ( which I am creating myself). It's proving to be far more difficult than I had first imagined ! Can anyone help me out please ? I will start out working on the basics and will update this post as I get further in.

My code for the generic linked list is as follows :

    public class NodeList<T> {
  private Comparable head;
  private NodeList tail;
  public NodeList( Comparable item, NodeList list ) {
    head = item;
    tail = list;
  }

}

I am trying to access this class in another class I have made, which is as follows :

public class MyList<T> {

  private NodeList<T> nodes;
  private int size;
  public MyList( ) { 
    nodes = null; 
  }

  public MyList(T[] array ){
    for(int countArray = 0; countArray <= array.length() ; countArray++) {
      nodes= new NodeList( value, nodes );
      size++;
    }
  }

which should add generic items from an array, using a linked list. Unfortunately, it doesn't and this is the first problem I have encountered. I am getting the error :

cannot find symbol : method length().

Can someone give me some advice on how I could fix this?

Many thanks!

2
  • Why are you trying to mergesort a linked list and not an array-backed list? Mergesort is meant for lists with O(1) (constant-time) element access, but linked lists have O(n) (linear-time) element access. Commented Mar 3, 2011 at 16:09
  • What is that method at the bottom doing? You're looping through an array but not using any of the values from it, setting and re-setting nodes multiple times, and setting size to the length of the passed-in array. I'm confused. Commented Mar 3, 2011 at 16:09

6 Answers 6

7

on the array you don't have a length() method but a length member : array.length

Additionally, you'll want to stop iterating before countArray reaches array.length and initialise size before using it:

final int arrayLength = array.length;
size = arrayLength;
nodes = null;
for(int i = 0; i < arrayLength; ++i) {
      nodes = new NodeList(array[i], nodes);
}

or

nodes = null;
size = array.length;
for(T element : array) {
      nodes = new NodeList(element, nodes);
}
Sign up to request clarification or add additional context in comments.

Comments

2

The method on a collections class is .size(), or on an array it is the .length property.

But you can loop through either of these with an "enhanced" for loop (aka foreach):

for( T element : array ) {
    nodes = new NodeList( value, nodes );
    size++;
}

1 Comment

This also get's rid of the potential ArrayIndexOutOfBoundsException :)
1

In addition to what others have posted, you might also want to use your generic parameter T:

public class NodeList<T> {
  private T head;
  private NodeList<T> tail;
  public NodeList( T item, NodeList list ) {
    head = item;
    tail = list;
  }
}

Comments

1

length is a field, not a method, on arrays. Remove the parentheses.

for(int countArray = 0; countArray <= array.length ; countArray++) {
  nodes= new NodeList( value, nodes );
  size++;
}

Here's a better way to write the whole constructor:

public MyList(T[] array ){
    nodes = null;
    for(T t : array) {
        nodes = new NodeList(t, nodes);
    }
    size = array.length;
}

Comments

1

If you want to make sure that only comparable items are possible:

public class NodeList<T extends Comparable<T> > {
  private T head;
  private NodeList<T> tail;
  public NodeList( T item, NodeList<T> list ) {
    head = item;
   tail = list;
  }
}

And

public class MyList<T extends Comparable<T>> {
...
}

Additionally, if your constructor uses var args, you get a more convenient way of creating a list:

public MyList(T... array ) {
  for( T item : array ) {
    nodes = new NodeList<T>(item, nodes); 
  }
  size = array.length;
}

That way you can call the constructor as follows:

new MyList<Long>(); //empty list
new MyList<Long>( 1L ); //one entry
new MyList<Long>( 1L, 2L, 3L ); //3 entries
Long[] array = new Long[] { 1L, 2L, 3L, 4L };
new MyList<Long>( array ); //use existing array

Comments

0

It is array.length not array.length().

for(int countArray = 0; countArray <= array.length ; countArray++) {

will resolve your compilation error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.