[英]Why can't I decrease for all elements in my ArrayList/LinkedList
Original question : 原始问题:
Originally, I want to decrease (1) for all elements in MyLinkedList. 最初,我想对MyLinkedList中的所有元素减少(1)。 But, since I cannot use foreach loop in MyLinkedList I first transferred all elements in MyLinkedList to ArrayList then with a foreach loop decreased (1) for all of the elements.
但是,由于我无法在MyLinkedList中使用foreach循环,因此我首先将MyLinkedList中的所有元素传输到ArrayList,然后对所有元素减少了foreach循环(1)。 But the thing is, I want to do this decrease operation per second.
但问题是,我想每秒执行一次此减少操作。
input : 5-6-7
output : 1 second : 4-5-6
2 second : 3-4-5 ...
My problem : 我的问题 :
The problem with my code is : it decreases (1) per second for the ith element of ArrayList. 我的代码的问题是:对于ArrayList的第i个元素,它每秒减少(1)。
input : 5-6-7
output : 1 second 4-6-7
2 second 4-5-6
...
Here's my code so far. 到目前为止,这是我的代码。
public void decreaseTime(MyLinkedList l1,MyLinkedList l2) {
ArrayList<Integer> arr = new ArrayList<Integer>();
Node tmp = l1.first;
/*originally i want to decrease 1 for all elements of mylinkedlist.
since i cannot
use foreach in mylinkedlist i first transferred
elements of mylinkedlist to an Arraylist arr.
then decreased 1 for each element.*/
//lltoarr does copy all mylinkedlist elements to ArrayList arr.
lltoarr(l1, arr);
while (tmp != null) {
// simple sleep method which pauses the program for 1 second.
sleep();
int j = 0;
//decrease operation for each element of arr
for (int t : arr) {
//this is a trick for my program to work. you may ignore the next line.
int c = tmp.data.length() - 1;
t--;
arr.set(j, t);
// timeNew gets the new time which is the edited t element
int timeNew = arr.get(j);
if (somethin) {
//copy timeNew to String. i must store the time as string in mylinkedlist.
String newData = tmp.data.substring(0, c - 2) + timeNew
+ "h";
tmp.data = newData;
System.out.println(tmp.data);
sleep();
tmp = tmp.next;
}
else {
String newData = tmp.data.substring(0, c - 1) + timeNew
+ "h";
tmp.data = newData;
System.out.println(tmp.data);
sleep();
tmp = tmp.next;
}
j++;
}
System.out.println(arr);
}
}
MyLinkedList class : MyLinkedList类:
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.LinkedList;
public class MyLinkedList {
Node first, last;
public void insertFirst(String s) {
Node n = new Node(s);
if (first == null) {
last = n;
}
n.next = first;
first = n;
}
public void insertLast(String s) {
Node n = new Node(s);
if (first == null) {
first = n;
} else {
last.next = n;
}
last = n;
}
public void deleteFirst() {
if (first.next == null) {
first = null;
last = null;
}
else if (first == null)
return;
else
first = first.next;
}
public void lltoarr(MyLinkedList ll, ArrayList<Integer> arr) {
Node tmp = ll.first;
while (tmp != null) {
String numberOnly = tmp.data.replaceAll("[^0-9]", "");
int time = Integer.parseInt(numberOnly);
arr.add(time);
tmp = tmp.next;
}
}
//decreaseTime method here
public void sleep() {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
public void print() {
Node tmp = first;
int i = 0;
while (tmp != null) {
System.out.print(tmp.data + " - ");
tmp = tmp.next;
}
}
public int nOfNodes() {
int n = 0;
Node tmp = first;
while (tmp != null) {
tmp = tmp.next;
n++;
}
return n;
}
public Node search(String s) {
Node tmp = first;
while (tmp != null) {
if (tmp.data == s) {
return tmp;
} else {
tmp = tmp.next;
}
}
return null;
}
public void llCopy(MyLinkedList ll, MyLinkedList llcopy,int i) {
llcopy.insertLast(ll.nthNode(i));
}
public void insertAfter(String s, Node n) {
Node nn = new Node(s);
nn.next = n.next;
n.next = nn;
}
}
How you are indexing the list is not going to work. 您如何为列表建立索引将无法正常工作。
If you want to iterate through the list and adjust each value I would suggest wrapping the Integer value with a holder that has a decrement function 如果要遍历列表并调整每个值,我建议将Integer值包装在具有减量功能的支架上
eg 例如
public IntegerHolder {
private int value;
....
public void decrement(){
this.value--;
}
}
Then you can do this 那你可以做
for(IntegerHolder value : list){
value.decrement();
}
Otherwise you could use a ListIterator and remove the old value and insert the decremented value, 否则,您可以使用ListIterator并删除旧值,然后插入减小的值,
for(ListIterator<Integer> iter = list.listIterator();iter.hasNext();){
int value = iter.next(); // get current value
iter.remove(); // remove it from list
iter.add(value-1); // insert decremented value where old value was
}
Personally I find the foreach loop for collections to be of limited value. 我个人发现集合的foreach循环价值有限。 Usually I want to access the iterator for some reason and it wont let me.
通常我出于某种原因要访问迭代器,但它不会让我使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.