Переназначьте `this` в классе Java ⇐ JAVA
-
Anonymous
Переназначьте `this` в классе Java
I'm just fooling around in Java right now, trying to implement something similar to a stack using linked lists.
class ListStack { int value; int size; ListStack next; public ListStack (int add) { this.size = 1; this.value = add; this.next = null; } public void push (int add) { this.next = this; this.value = add; this.size++; } public int pop() { if (this.size == 0) { throw new EmptyListStackException(); } int i = this.value; this = this.next; this.size--; return i; } public int size() { return this.size; } public int peek() { return this.value; } } Basically it's an insertion-in-front linked list which also removes from front. NetBeans pops an error when I try to do this = this.next; it says I cannot reassign final value this.
I'd like my final implementation to do something like the below:
ListStack var = new ListStack(5); //var is now 5 -> null var.push(3); //var is now 3 -> 5 -> null int val = varr.pop(); //var is now 5 -> null, val == 3 Commenting put that this = this.next code, the rest seems to work.
ListStack a = new ListStack(5); System.out.println(a.size()); //prints 1 System.out.println(a.peek()); //prints 5 a.push(4); System.out.println(a.size()); //prints 2 System.out.println(a.peek()); //prints 4 a.push(6); System.out.println(a.size()); //prints 3 System.out.println(a.peek()); //prints 6 a.push(1); System.out.println(a.size()); //prints 4 System.out.println(a.peek()); //prints 1 //a is 1 -> 6 -> 4 -> 5 -> null
Источник: https://stackoverflow.com/questions/230 ... java-class
I'm just fooling around in Java right now, trying to implement something similar to a stack using linked lists.
class ListStack { int value; int size; ListStack next; public ListStack (int add) { this.size = 1; this.value = add; this.next = null; } public void push (int add) { this.next = this; this.value = add; this.size++; } public int pop() { if (this.size == 0) { throw new EmptyListStackException(); } int i = this.value; this = this.next; this.size--; return i; } public int size() { return this.size; } public int peek() { return this.value; } } Basically it's an insertion-in-front linked list which also removes from front. NetBeans pops an error when I try to do this = this.next; it says I cannot reassign final value this.
I'd like my final implementation to do something like the below:
ListStack var = new ListStack(5); //var is now 5 -> null var.push(3); //var is now 3 -> 5 -> null int val = varr.pop(); //var is now 5 -> null, val == 3 Commenting put that this = this.next code, the rest seems to work.
ListStack a = new ListStack(5); System.out.println(a.size()); //prints 1 System.out.println(a.peek()); //prints 5 a.push(4); System.out.println(a.size()); //prints 2 System.out.println(a.peek()); //prints 4 a.push(6); System.out.println(a.size()); //prints 3 System.out.println(a.peek()); //prints 6 a.push(1); System.out.println(a.size()); //prints 4 System.out.println(a.peek()); //prints 1 //a is 1 -> 6 -> 4 -> 5 -> null
Источник: https://stackoverflow.com/questions/230 ... java-class
Мобильная версия