U
U
Username2015-07-16 20:35:39
Java
Username, 2015-07-16 20:35:39

How are lists arranged in Java?

Before that, I worked with the structure only in C, and I figured out one difference, if you break the connection of an element, then the garbage collector will clean up after you.
But there are a lot of questions, what is the difference between a structure and an object? Adding an element works in such a way that we first create a head with a null reference value, after adding the head becomes not null, but a reference to the added object?
I would understand this whole process more clearly if you could explain the lines in the following code:

public class List {
  
  
  public static class Node {
    public int data;
    public Node next;
  }

  
  private Node head;
  
  public  List() {
    head = null;		
  }
  

  
  @Override
  public String toString() {		
    String res = "<< ";		
    for (Node p = head; p != null; p = p.next) {
      res = res + p.data + " ";			
    }
    res = res + ">>";
    return res;
  }

  
  public  void addElementToHead(int value) {
    Node newNode = new Node();
    newNode.next = head;
    newNode.data = value;
    head = newNode;		
  }	
  
  
  
  
  public void deleteElement(Node delNode) {
    
    if (delNode == null) {
      return;
    }
    
    
    if (delNode == head) {
      head = head.next;
      return;
    } else {
      
      for (Node p = head; p != null; p = p.next) {
        if (p.next == delNode) {
          p.next = delNode.next;
        }		
      }			
    }		
    
  }
  
  
  
  
  public  void clear() {
    head = null;
  }
    

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evhen, 2015-07-16
@dalv_happy

The deleteElement method is not implemented correctly. The '==' operation compares references to objects, not the objects themselves.
For example, if you have a Node object in the list with the field value data = 10, and you want to delete it
Node delNode = new Node();
delNode.data = 10;
deleteElement(delNode); // nothing will be deleted
because the delNode reference and the reference in the list refer to different objects.
In Java, for comparison, they override the equals method, in which they implement the comparison of class fields.
This is how it should look

if (delNode.equals(head)) {
      head = head.next;
      return;
    } else {
      
      for (Node p = head; p != null; p = p.next) {
        if (delNode.equals(p.next)) {
          p.next = delNode.next;
        }		
      }

The difference between a structure and a class is that the class allows you to implement inheritance, encapsulation and polymorphism.

B
bromzh, 2015-07-17
@bromzh

Java has several lists. There is a List interface, there are several implementations of it. The most popular are ArrayList (essentially a dynamic array), there is LinkedList - a linked list. If you google a little, you will find their source codes, they will speak for themselves.
In the future, to implement your list, it's better to implement a standard interface.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question