【问题标题】:print() function that prints the contents of each element of your listprint() 函数打印列表中每个元素的内容
【发布时间】:2014-09-17 16:15:37
【问题描述】:

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

这里是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 文件

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

public void print { 

}   

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

感谢任何帮助。


更新:我将在这里发布 Llist.java、list.java 和 link.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 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(); 
} 

链接.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; } 
} 

【问题讨论】:

  • s_list.foreach(System.out::println) 怎么样?如果不发布 LList 的代码,或者至少不发布它的声明,我们就无能为力了。

标签: java list data-structures linked-list element


【解决方案1】:

我们需要查看 LList.java 类的内部......但现在我将假设 LList 扩展了 List [或 ArrayList 等......]

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 的外观... [this.size()] 指的是 List 或 ArrayList 类 [如果您扩展了它...]。

如果你没有扩展 List 或类似的东西,你总是可以这样做:

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

但与往常一样,您可以采取简单的方法并这样做:

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

【讨论】:

  • 嘿,非常感谢您的指导。我已经为我在这个项目中拥有的其他核心 java 文件添加了代码。请看一下,看看您是否可以弄清楚需要更改的内容,因为我已经完成的其他.java 中没有方法 Size。
【解决方案2】:

根据您的 cmets 修改答案:

public void print() {
        Link<E> currentNode  = head; //Sets starting node to first node in list
        while (currentNode != tail) { //Checks if current node is equal to last node
            System.out.println(currentNode.element()); //Prints currentNodes's element
            currentNode = currentNode.next(); //Sets currentNode to next node in list
        }
        System.out.println(tail.element()); //Prints last node in list      
}

注意:Link&lt;E&gt;code 中的某些 cmets 与您的函数实际执行的操作不匹配。

【讨论】:

  • s_list 无法解析为变量是我得到的错误。
  • 你的 print() 函数中有 for 循环吗?您是否将 s_list 传递给函数?
  • 我已经查看了更多代码并发布了修改后的答案。让我知道这是否有效。
  • “只能迭代到数组或实例”是我现在在打印方法上遇到的错误。
  • 您可以更改您的列表以实现 Iterable,这将允许 for 循环工作,或者您可以尝试以下方式。从链表中的头节点开始并检索其值(您创建的 getValue() 函数)。然后转到它指向的节点并重复。 System.out.print() 检索到的每个值,直到下一个节点指向 null;
猜你喜欢
  • 2015-12-16
  • 2013-07-19
  • 2021-04-09
  • 1970-01-01
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-09
  • 1970-01-01
相关资源
最近更新 更多