在浏览器历史系统中乘以 for 循环
multiplying for loop in a browser history system
我想,我一直在为一个项目编写一个网络浏览器,我发现我无法让我的历史系统按预期工作,目前我发现我的历史项目正在复制,我的历史记录在基于 for 循环样式金字塔的会话之间自我复制,其中金字塔的大小是我上次访问的页面数的 n-1:
页重复 |上次访问的页面
1 1
12 2
123 3
1234 4
每当我访问一个新页面时调用此方法并且上半部分的 if 语句仅 运行 一次,当浏览器启动并且可以从 CSV 文件中恢复上一个会话的历史记录时它存储在.
代码应该在每次访问页面时创建一个 jmenuitem,然后将其添加到 jmenu,这样做很好,但是,还应该将 link 添加到列表。然后将该列表附加到 csv 进行存储。
public class FileBar extends JMenuBar {
int tracker = 0;
File histPath = new File("history.csv");
JMenu history = new JMenu("History");
List<String> histStore = new ArrayList<String>();
public void createhistory(String webAddress) {
try {
List<String> histFeedback = new ArrayList<String>();
writer = new FileWriter(histPath, true);
if (tracker < 1) {
// system to retrieve information from csv file upon launch of program
}
JMenuItem button = new JMenuItem(webAddress);
history.add(button);
button.addActionListener(new ActionListener() {
// ...
});
histStore.add(webAddress);
int i = 0;
for (i = 0; i < histStore.size(); i++) {
writer.append(histStore.get(i));
writer.append(",");
}
writer.flush();
} catch (Exception e) {}
}
}
假设金字塔问题发生在您的 csv 中,似乎正在发生的事情是您每次访问页面时都将历史列表写入 csv。您访问的第一个页面会将页面附加到列表中,然后将其写入 csv。您访问的第二个页面会将页面附加到列表中,然后将完整列表写入 csv,因为此代码:
for (i = 0; i < f.histStore.size(); i++) {
writer.append(f.histStore.get(i));
writer.append(",");
}
您需要覆盖 csv 中的行,或者只附加您最近的项目。
好的,这就是(我认为)问题所在。每次您访问一个页面时,您似乎都在 附加 整个历史记录 到 CSV 中的行。
我不知道 f.histStore
来自哪里,但我认为它是从 CSV 中的行创建的。所以如果CSV里面有5个地址,那么好像是f.histStore.size() == 5
.
因此,当您转到某个页面时,将该地址附加到 f.histStore
:
f.histStore.add(webAddress);
好的,到目前为止看起来还不错。但是,您 append f.histStore
到最初读取的行:
for (i = 0; i < f.histStore.size(); i++) {
writer.append(f.histStore.get(i));
writer.append(",");
}
因此您已将整个列表附加到现有列表。所以这会导致重复模式,像这样,其中 a
、b
和 c
是地址:
a
aab
aabaabc
如果是这种情况,有一个简单的解决方案:只将最后一个地址写入文件。将写循环替换为:
int lastIndex = f.histStore.size() - 1;
writer.append(f.histStore.get(lastIndex));
writer.append(",");
这样行吗?如果不是,错误的输出是什么?
我想,我一直在为一个项目编写一个网络浏览器,我发现我无法让我的历史系统按预期工作,目前我发现我的历史项目正在复制,我的历史记录在基于 for 循环样式金字塔的会话之间自我复制,其中金字塔的大小是我上次访问的页面数的 n-1:
页重复 |上次访问的页面
1 1
12 2
123 3
1234 4
每当我访问一个新页面时调用此方法并且上半部分的 if 语句仅 运行 一次,当浏览器启动并且可以从 CSV 文件中恢复上一个会话的历史记录时它存储在.
代码应该在每次访问页面时创建一个 jmenuitem,然后将其添加到 jmenu,这样做很好,但是,还应该将 link 添加到列表。然后将该列表附加到 csv 进行存储。
public class FileBar extends JMenuBar {
int tracker = 0;
File histPath = new File("history.csv");
JMenu history = new JMenu("History");
List<String> histStore = new ArrayList<String>();
public void createhistory(String webAddress) {
try {
List<String> histFeedback = new ArrayList<String>();
writer = new FileWriter(histPath, true);
if (tracker < 1) {
// system to retrieve information from csv file upon launch of program
}
JMenuItem button = new JMenuItem(webAddress);
history.add(button);
button.addActionListener(new ActionListener() {
// ...
});
histStore.add(webAddress);
int i = 0;
for (i = 0; i < histStore.size(); i++) {
writer.append(histStore.get(i));
writer.append(",");
}
writer.flush();
} catch (Exception e) {}
}
}
假设金字塔问题发生在您的 csv 中,似乎正在发生的事情是您每次访问页面时都将历史列表写入 csv。您访问的第一个页面会将页面附加到列表中,然后将其写入 csv。您访问的第二个页面会将页面附加到列表中,然后将完整列表写入 csv,因为此代码:
for (i = 0; i < f.histStore.size(); i++) {
writer.append(f.histStore.get(i));
writer.append(",");
}
您需要覆盖 csv 中的行,或者只附加您最近的项目。
好的,这就是(我认为)问题所在。每次您访问一个页面时,您似乎都在 附加 整个历史记录 到 CSV 中的行。
我不知道 f.histStore
来自哪里,但我认为它是从 CSV 中的行创建的。所以如果CSV里面有5个地址,那么好像是f.histStore.size() == 5
.
因此,当您转到某个页面时,将该地址附加到 f.histStore
:
f.histStore.add(webAddress);
好的,到目前为止看起来还不错。但是,您 append f.histStore
到最初读取的行:
for (i = 0; i < f.histStore.size(); i++) {
writer.append(f.histStore.get(i));
writer.append(",");
}
因此您已将整个列表附加到现有列表。所以这会导致重复模式,像这样,其中 a
、b
和 c
是地址:
a
aab
aabaabc
如果是这种情况,有一个简单的解决方案:只将最后一个地址写入文件。将写循环替换为:
int lastIndex = f.histStore.size() - 1;
writer.append(f.histStore.get(lastIndex));
writer.append(",");
这样行吗?如果不是,错误的输出是什么?