【发布时间】:2015-04-25 12:18:44
【问题描述】:
原问题:
最初,我想为 MyLinkedList 中的所有元素减少 (1)。但是,由于我不能在 MyLinkedList 中使用 foreach 循环,所以我首先将 MyLinkedList 中的所有元素转移到 ArrayList,然后为所有元素减少一个 foreach 循环 (1)。但问题是,我想每秒执行一次减少操作。
input : 5-6-7
output : 1 second : 4-5-6
2 second : 3-4-5 ...
我的问题:
我的代码的问题是:ArrayList 的第 i 个元素每秒减少 (1)。
input : 5-6-7
output : 1 second 4-6-7
2 second 4-5-6
...
这是我目前的代码。
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 类:
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;
}
}
【问题讨论】:
-
您是否尝试过单步执行代码以查看问题出在哪里?这是调试器应该是您的第一个调用点的问题
-
所以你根据
for-each的兼容性来决定使用哪种数据结构?为什么不坚持使用LinkedList并使用while 循环呢?将每个元素复制到另一个列表只是为了使用for-each循环不是要走的路。 -
@JonnyHenly 我必须使用 LinkedList。但我还必须从每个元素中减少 (1),所以我想出了这个想法。
-
但是你没有使用
LinkedList,你使用的是MyLinkedList。不过,如果您必须编写自己的链表实现(可能是针对一个类),为什么不直接将其设为Iterable以便您可以使用增强的 for 循环呢? -
@Kaan 这不是一个坏主意,但也不是一个好主意。 BevynQ 的回答是一个很好的方法。
标签: java arraylist foreach iteration