print()函数,用于打印列表中每个元素的内容 [英] print() function that prints the contents of each element of your list

查看:1633
本文介绍了print()函数,用于打印列表中每个元素的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我正在尝试编写一个print语句,允许我在运行driver.java时将每行的元素打印为println作为输出。而对于我的生活,我无法弄清楚如何做到这一点。任何帮助将不胜感激。

Basically i'm trying to write a print statement that will allow me to print the elements per line as a println as the output when i run the driver.java. And for the life of me i cannot figure out how to do it. Any help will be appreciated.

这里是driver.java

here is the driver.java

public class Driver {

public static void main(String args[]){



    LList<String> s_list = new LList<String>();

    s_list.insert("New York, 8.4M");
    s_list.insert("Los Angeles 3.8M");
    s_list.insert("Chicago, 2.7M");
    s_list.insert("Houston, 2.1M");
    s_list.insert("Philadelphia, 1.55M");
    s_list.insert("Phoenix, 1.51M");
    s_list.append("San Antonio, 1.4M");
    s_list.append("San Diego, 1.35M");
    s_list.append("Dallas, 1.25M");
    s_list.append("San Jose, 0.998M");
    s_list.append("Austin, 0.88M");
    s_list.append("Indianapolis, 0.84M");
    s_list.append("Jacksonville, 0.84M");
    s_list.append("San Francisco, 0.83M");
    s_list.append("Columbus, 0.82M");
    s_list.append("Charlotte, 0.79M");

    s_list.print();
    s_list.moveToPos(3);
    s_list.remove();
    s_list.print();
    s_list.moveToEnd();
    s_list.remove();
    s_list.print();
    s_list.moveToStart();
    s_list.remove();
    s_list.remove();
    s_list.print();
    s_list.clear();
    s_list.print();


}

}

我有一个名为LList.java的java文件

and I have a java file named LList.java

我正在尝试编写一个print方法,它将print()函数打印出每个元素的内容。你的清单;每行打印一个元素。

where I am trying to write a print method where it will print() function that prints the contents of each element of your list; print one element per line.

public void print { 

}   

那么,我将如何打印每行s_list行中的元素作为输出。

So, How will i print the elements in "s_list" line per line as output.

感谢任何帮助。

更新:我将发布Llist.java,list.java &安培; link.java here

UPDATE: I am going to post Llist.java,list.java & link.java here

Llist.java

Llist.java

 /** Linked list implementation */ 

 class LList<E> implements List<E> { 
 private Link<E> head; // Pointer to list header 
 private Link<E> tail; // Pointer to last element 
protected Link<E> curr; // Access to current element 
 private int cnt; // Size of list 
 /** Constructors */ 
 LList(int size) { this(); } // Constructor -- Ignore size 
 LList() { 
 curr = tail = head = new Link<E>(null); // Create header 
  cnt = 0; 
 } 
/** Remove all elements */ 
public void clear() { 
head.setNext(null); // Drop access to links 
curr = tail = head = new Link<E>(null); // Create header 
cnt = 0; 
} 
/** Insert "it" at current position */ 
public void insert(E it) { 
curr.setNext(new Link<E>(it, curr.next())); 
if (tail == curr) tail = curr.next(); // New tail 
cnt++; 
} 
/** Append "it" to list */ 
public void append(E it) { 
tail = tail.setNext(new Link<E>(it, null)); 
cnt++; 
} 
/** Remove and return current element */ 
public E remove() { 
if (curr.next() == null) return null; // Nothing to remove 
 E it = curr.next().element(); // Remember value 
 if (tail == curr.next()) tail = curr; // Removed last 
curr.setNext(curr.next().next()); // Remove from list 
cnt--; // Decrement count 
return it; // Return value 
} 
/** Set curr at list start */ 
public void moveToStart() 
{ curr = head; } 

/** Set curr at list end */ 
public void moveToEnd() 
{ curr = tail; } 
/** Move curr one step left; no change if now at front */ 
public void prev() { 
if (curr == head) return; // No previous element 
Link<E> temp = head; 
// March down list until we find the previous element 
while (temp.next() != curr) temp = temp.next(); 
curr = temp; 
} 
/** Move curr one step right; no change if now at end */ 
public void next() 
{ if (curr != tail) curr = curr.next(); } 
/** @return List length */ 
public int length() { return cnt; } 
/** @return The position of the current element */ 
public int currPos() { 
    Link<E> temp = head; 
    int i; 
    for (i=0; curr != temp; i++) 
        temp = temp.next(); 
    return i; 
} 
/** Move down list to "pos" position */ 
public void moveToPos(int pos) { 
    assert (pos>=0) && (pos<=cnt) : "Position out of range"; 
    curr = head; 
    for(int i=0; i<pos; i++) curr = curr.next(); 
} 
/** @return Current element value */ 
public E getValue() { 
    if(curr.next() == null) return null; 
    return curr.next().element(); 
}

public void print()
{

}   

}

List.java

List.java

/** List ADT */ 
public interface List<E> { 
    /** Remove all contents from the list, so it is once again 
empty. Client is responsible for reclaiming storage 
used by the list elements. */ 
    public void clear(); 
    /** Insert an element at the current location. The client 
must ensure that the list�s capacity is not exceeded. 
@param item The element to be inserted. */ 
    public void insert(E item); 
    /** Append an element at the end of the list. The client 
must ensure that the list�s capacity is not exceeded. 
@param item The element to be appended. */ 
    public void append(E item); 
    /** Remove and return the current element. 
@return The element that was removed. */ 
    public E remove(); 
    /** Set the current position to the start of the list */ 
    public void moveToStart(); 
    /** Set the current position to the end of the list */ 
    public void moveToEnd(); 
    /** Move the current position one step left. No change 
if already at beginning. */ 
    public void prev(); 
    /** Move the current position one step right. No change 
if already at end. */ 
    public void next(); 
    /** @return The number of elements in the list. */ 
    public int length(); 
    /** @return The position of the current element. */ 
    public int currPos(); 
    /** Set current position. 
@param pos The position to make current. */ 
    public void moveToPos(int pos); 
    /** @return The current element. */ 
    public E getValue(); 
} 

Link.java

Link.java

/** Singly linked list node */ 
class Link<E> { 

    private E element; // Value for this node 
    private Link<E> next; // Pointer to next node in list 
    // Constructors 
    Link(E it, Link<E> nextval) 
    { element = it; next = nextval; } 
    Link(Link<E> nextval) { next = nextval; } 
    Link<E> next() { return next; } // Return next field 
    Link<E> setNext(Link<E> nextval) // Set next field 
    { return next = nextval; } // Return element field 
    E element() { return element; } // Set element field 
    E setElement(E it) { return element = it; } 
} 


推荐答案

我们需要看看在LList.java类中...但是现在我将假设LList扩展List [或ArrayList等等]。

We will need to see inside the class of LList.java... but for now i am going to assume LList extends List [or ArrayList, etc..]

public void print
{
     for(int i = 0; i < this.size(); i++)  //this really depends on how you store your list
          System.out.println(this.get(i));
}

这一切都取决于你的LList.java看起来如何... [这个.size()]引用List或ArrayList类[如果你扩展它...]。

This all depends on how your LList.java looks though... [this.size()] refers to the List or ArrayList class [if you extended it...].

如果您没有按照这些方式扩展List或其他内容,您可以随时执行:

If you are not extending List or something along those lines, you could always do:

public void print
{
     for(int i = 0; i < storingArray.size(); /*or .length*/ i++)
          System.out.println(storingArray.get(i)); /*or storingArray[i]*/
}

但一如既往,你可以采取简单的方法就是:

But as always, you could take the easy way and just do:

list.foreach(System.out::println); //must have Java 8.

这篇关于print()函数,用于打印列表中每个元素的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆