I have a problem in my java code the problem is when I try to use methods multiple times
here's an example
/* works */
PureListString t = L.addFirst("abc");
System.out.println(t.get(0));
/* don't work */
L.addFirst("abc");
System.out.println(L.get(0))
These are my functions
public PureListString(String elt) {
this.first = elt;
this.tail = EMPTY_LIST;
this.size = 1;
}
public PureListString addFirst(String elt) {
PureListString newCell = new PureListString(elt);
PureListString tmp = this;
tmp.size++;
newCell.tail = tmp;
tmp = newCell;
return tmp;
}
/* same as addFirst() */
public PureListString removeFirst() {
if (this.isEmpty()){
return EMPTY_LIST;
}
PureListString tmp = this;
tmp.size--;
tmp = this.tail;
return tmp;
}
addLast method works perfectly
public PureListString addLast(String elt) {
PureListString newCell = new PureListString(elt);
PureListString tmp = this;
tmp.size++;
while (tmp.tail != EMPTY_LIST)
tmp = tmp.tail;
tmp.tail = newCell;
return tmp;
}
public static void main(String[] args){
List<String> list1 = Stream.of("January", "February", "March", "April", "May")
.collect(Collectors.toList());
PureListString L = new PureListString(list1);
//PureListString t = L.addFirst("abc");
//PureListString h = L.addFirst("a");
//PureListString t = L.addFirst("abc");
L.addLast("x");
L.addLast("y");
L.addLast("z");
System.out.println(L.contains("y"));
System.out.println(L.contains("z"));
System.out.println(L.contains("May"));
System.out.println(L.indexOf("z"));
System.out.println(L.get(7));
PureListString t = L.removeFirst();
System.out.println(t.get(0));
System.out.println(L.size);
}
The problem is addFirst() and removeFirst() methods the same problem occurs in both of these methods I have always to do PureListString t = L.method().
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…