For a loop with a shared array?

Returning to my main ADT material here, and trying to kill two birds with one stone, learning Java, while I'm trying to write a simple merge sort algorithm with a common linked list (which I create myself). It turned out to be much more complicated than I imagined! Can someone help me please? I will begin work on the basics and will update this post until I get further access.

My code for a 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 that I made, which looks like this:

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 common elements from the array using a linked list. Unfortunately, this is not the case, and this is the first problem I have encountered. I get an error message:

: length().

- , ?

!

+3
6

length(), : array.length

, , countArray array.length :

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

nodes = null;
size = array.length;
for(T element : array) {
      nodes = new NodeList(element, nodes);
}
+7

.size(), .length.

"" (aka foreach):

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

length - , , . .

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

:

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

, , T:

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

, :

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;
  }
}

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

Also, if your constructor uses var args, you get a more convenient way to create a list:

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

Thus, 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
+1
source

This is array.length not array.length ().

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

will resolve your compilation error.

0
source

Source: https://habr.com/ru/post/1796077/


All Articles