我有一个问题,我在网上找了很多,但找不到一个例子。但是在java中创建一个String trim()方法(删除前导/尾随空格),我知道它的基本代码是:
public LString trim(){
int i = this.size;
int j = 0;
int k = this.offset;
char[] arrayOfChar = this.data;
while ((j < i) && (arrayOfChar[(k + j)] <= ' '))
++j;
while ((j < i) && (arrayOfChar[(k + i - 1)] <= ' '))
--i;
return (((j > 0) || (i < this.size)) ? substring(j, i) : this);
}
但是,你会如何写同样的方法,但应用于链表?更具体地说,是一个使用Node类的链表
以下是我所做的。。。。如果这是错误的,请纠正我。。。我将包括与该问题相关的课堂信息。
public class LString{
private Node front = null; //first val in list
private Node back; //last val in list
private int size = 0;
private int i;
private int offset;
public LString(){
//construct empty list
Node LString = new Node();
front = null;
}
.......//skip down some methods to this one
//returns new lstring that is slice of lstring
//contains an endIndex as well
public LString substring(int beginIndex, int endIndex){
Node current = this.front;
int size = 0;
while(current != null && size < beginIndex){
size++;
current = current.getNext();
}
front = new Node();
front.setData(current.getData());
Node ocurrent = front;
while(current != null && size < endIndex){
current = current.getNext();
Node curr2 = new Node();
curr2.setData(current.getData());
ocurrent.setNext(curr2);
ocurrent = curr2;
size++;
}
ocurrent.setNext(null); //set next val to null to term string
return this;
}
public LString trim(){
String lstr;
int i = this.size;
int m = this.offset;
int k = charAt(m);
Node current = front;
while(current != null){
current = current.getNext();
if(current.data > 'u0020'){
return this;
} else if(current.data < 'u0020'){
LString lstring = new LString(); //this worked!?
return lstring;
}
}
return this.substring(k, m+1);
}
//My Node class:
public class Node{
public char data;
public Node next;
//constructors from page 956
public Node()
{
this(' ',null); //' ' is null char for java
}
public Node(char initialData, Node initialNext)
{
data = initialData;
next = initialNext;
}
}
(如果你不熟悉节点类,它基本上只是创建一个单独链接的节点,用作链表类中数据之间的链接)
我从来没有见过任何例子,所以我想问问社区。
假设
- 通过修剪要删除null的前导元素和尾部元素的列表
- 在"使用Node类的链表"下,您指的是java.util.LinkedList
您应该记住,在java中,LinkedList的内部实现是不公开的(注意:java.util.LinkedList.Node具有私有访问修饰符),所有修改都是通过迭代器和LinkedList本身的方法执行的。
实施方式为:
public static void trim (LinkedList list){
if (list == null || list.size() == 0) return;
Object element = null;
ListIterator i = list.listIterator();
while (i.hasNext() && element == null) {
element = i.next();
if (element == null) {
i.remove();
}
}
element = null;
i = list.listIterator(list.size());
while (i.hasPrevious() && element == null) {
element = i.previous();
if (element == null) {
i.remove();
}
}
}
但是,如果您正在通过链表重新实现可变字符串作为练习(如果不是练习,那么就到此为止,使用StringBuilder或StringBuffer),然后,假设您使用双链表实现它,它将如下所示:
EDIT:我的坏,你可以迭代到第一个非空元素,并直接设置对它的引用,更新算法
- 获取第一个元素
- 当提取的元素为空时,提取下一个
- 将head引用设置为最后一个提取的元素,将最后一个获取的元素的prev参考设置为null
- 获取最后一个元素
- 当提取的元素为空时,提取上一个
- 将尾部引用设置为最后一个提取的元素,将最后一个获取的元素的下一个引用设置为null
UPDATE使用您提供的代码,尝试这样的操作(由于您使用的是单链表,它与上面描述的略有不同):
public void trim(){
//early out if empty
if (front == null || back==null) return;
Node current = front;
//looking for the first non-empty element
while(current != null && current.data<'u0020' ){
current = current.next;
}
//left trim
this.front = current;
//looking for last non-empty element
while (current!=null&¤t.next!=null&¤t.next.data>'u0020'){
current = current.next;
}
//right trim
this.back = current;
if (current!=null){
current.next = null;
}
}
假设您只想修剪LinkedList中的每个String,为什么不迭代每个项呢?
LinkedList<String> myNodes = new LinkedList<String>();
myNodes.add('This is a node ');
myNodes.add(' another node '));
for (String s : myNodes){
s.trim();
}