简体   繁体   中英

How to print a particular element from a linked list

I am trying to print the IP address from the information stored in the LinkedList but am a bit confused on how to do that since the IP address and PC1 are stored together. The IP address and PC1 are stored together.

PlsWork :

import java.net.InetAddress;

public class PlsWork {

    private InetAddress IP;
    private String PC;

    public PlsWork(InetAddress IP, String PC){
        this.IP=IP;
        this.PC=PC;
    }

    public InetAddress getIP(){
        return IP;
    }

    public String getPC(){
        return PC;
    }

    @Override
    public String toString() {
        return IP + " " + PC;
    }
}

WorkPls :

import java.util.LinkedList;

public class workPls {
    private LinkedList  List= new LinkedList();

    public void addNode(PlsWork st){
        List.add(st);
    }

    public LinkedList getList(){
        return List;
    } 

    public void print(){
        System.out.println(List);
    }

}

Main class:

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Linked {

    public static void main(String[] args) {
        workPls oks = new workPls();
        try {
            DatagramSocket socket = new DatagramSocket(Integer.parseInt( args[0]));
            socket.setSoTimeout(0);
            while(true) {
                byte []buffer = new byte[1024];
                DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
                socket.receive(packet);
                PlsWork  ok = new PlsWork(packet.getAddress(),"pc1");
                oks.addNode(ok);
                oks.print();
            }
        }
        catch (Exception error){
            error.printStackTrace();
        } 
    }
}

How can I change the print to be able to do that?

When you call System.out.println() , it will atomatically call toString , so you can override toString to make it return only IP:

@Override
public String toString() {
    return IP;
}

PS:

It is a good name way to make the class name start with upper case letter, you can repalce workPls with WorkPls .

Instead of relying on LinkedList.toString() to print the list for you (which in turn will call PlsWork.toString() on each item in the list), you can manually iterate through the list and print each item as desired. For example:

public void print() {
    StringBuilder b = new StringBuilder();
    for(PlsWork w : List) {
        if(b.length>0) {
            //add a comma between entries, but not before the first (i.e. only if builder is empty)
            b.append(", ");
        }
        b.append(w.getIP());
    }
    //following adds curly braces front and end, if desired
    b.append('}');
    b.insert(0, '}');

    //finally print to sysout
    System.out.println(b.toString());
}

The above would print something like:

{1.1.1.1, 2.2.2.2, 3.3.3.3}

(Assuming 3 different entries with those IP addresses)

If you want to display only the IP of the items contained in your linkedList when you print it, change the body of the method print of your workPls class to display every item IP.

public void print(){
    String output = "{";
    for(PlsWork item : List) {
        output += item.getIP().toString();
        output += ", ";
    }
    output += "}";
    System.out.println(output);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM