我目前有以下代码将数据从txt文件读取到对象。但是ArrayList无法正常工作。
public static void load_Recipes() {
// Read file and load data into ingredient object
String[] current_line = new String[7];
// Variables for the ingredient object attributes
int recipeID;
String recipeName;
int recipePrepTime;
String recipeURL;
String recipeCuisine;
String recipeType;
double recipeRating;
String recipeIngredients;
ArrayList<Integer> recipeContainsID = new ArrayList<Integer>();
//String cleanString[] = new String[20];
//ArrayList<String> stringInt = new ArrayList<String>();
try {
int i = 0;
BufferedReader my_reader = new BufferedReader(new FileReader("recipes.txt"));
String input_line; //variable to read line by line
while( (input_line=my_reader.readLine()) != null){
current_line = input_line.split("\t"); //split the line at the tab
recipeID = Integer.parseInt(current_line[0]);
recipeName = current_line[1];
recipePrepTime = Integer.parseInt(current_line[2]);
recipeURL = current_line[3];
recipeCuisine = current_line[4];
recipeType = current_line[5];
recipeRating = Double.parseDouble(current_line[6]);
recipeIngredients = current_line[7];
System.out.println("The current value of recipeIngredients is: " + recipeIngredients); // Checking current value, removed when working
String clean1[] = recipeIngredients.replaceAll("\\[|\\]|","").split(",");
for(int j = 0; j < clean1.length; j++) {
System.out.print(clean1[j] + ", "); // Checking the current value, removed when working
recipeContainsID.add(Integer.parseInt(clean1[j]));
}
// test
all_recipes[i] = new Recipe(recipeID, recipeName, recipePrepTime, recipeURL, recipeCuisine, recipeType, recipeRating, recipeContainsID);
System.out.println("Next object.");
i++;
}
my_reader.close();
}
catch(IOException e){
System.out.println("can't read file");
}
}
文本文件包含以下内容:
1 Spaghetti 60 wwww.cooking.nl italiaans tussengerecht 1.0 [1,2,3,4]
2 Lasagna 30 wwww.cooking.nl italiaans tussengerecht 3.2 [5,6,7,8]
3 Groente 40 wwww.cooking.nl italiaans hoofdgerecht 3.1 [7,3,8,4]
4 Risotto 90 wwww.cooking.nl italiaans hoofdgerecht 4.1 [3,2,5,4]
每当我尝试将[]之间的最后一个整数写入对象ArrayList时,它们都会被添加到1个列表中。控制台中的输出:
1 Spaghetti 60 wwww.cooking.nl itialiaans tussengerecht 1.0 [1, 2, 3, 4, 5, 6, 7, 8, 7, 3, 8, 4, 3, 2, 5, 4]
2 Lasagna 30 wwww.cooking.nl itialiaans tussengerecht 3.2 [1, 2, 3, 4, 5, 6, 7, 8, 7, 3, 8, 4, 3, 2, 5, 4]
3 Groente 40 wwww.cooking.nl itialiaans hoofdgerecht 3.1 [1, 2, 3, 4, 5, 6, 7, 8, 7, 3, 8, 4, 3, 2, 5, 4]
4 Risotto 90 wwww.cooking.nl itialiaans hoofdgerecht 4.1 [1, 2, 3, 4, 5, 6, 7, 8, 7, 3, 8, 4, 3, 2, 5, 4]
打印代码:
public static void view_Recipes() {
for(int i=0;i<Recipe.getNum_recipes();i++)
System.out.println(all_recipes[i].getRecipeID() + "\t" + all_recipes[i].getRecipeName() + "\t" + all_recipes[i].getRecipePrepTime() + "\t" + all_recipes[i].getRecipeURL() + "\t" + all_recipes[i].getRecipeCuisine() + "\t" + all_recipes[i].getRecipeType() + "\t" + all_recipes[i].getRecipeRating() + "\t" + all_recipes[i].getRecipeContainsID());
}
答案 0 :(得分:0)
您需要在处理下一行配方之前声明arraylist,即在while循环内部。否则,相同的arraylist会不断累积,因为它是同一个对象,它将为每一行包含相同的值。
移动此行
循环中的 ArrayList<Integer> recipeContainsID = new ArrayList<Integer>();
。
while( (input_line=my_reader.readLine()) != null) {
ArrayList<Integer> recipeContainsID = new ArrayList<Integer>();
....
...
...
答案 1 :(得分:0)
这是因为您的import numpy as np
from scipy import sparse
import matplotlib
matplotlib.use('pdf')
import matplotlib.pyplot as plt
import networkx as nx
if __name__=='__main__':
hour_dense = np.zeros((986, 986))
hour_dense[80, 409] = 1
hour_dense[80, 891] = 1
hour_1 = sparse.csr_matrix(hour_dense)
edges = nx.from_scipy_sparse_matrix(hour_1)
options = {
'node_color': 'black',
'node_size': 3,
'line_color': 'red',
'linewidths': 1,
'edge_color':'red',
'width': 0.1,
}
nx.draw(edges, **options)
plt.savefig('train_graph_hour_1.png')
ArrayList
不在循环迭代中。
receipeContainsID
在读取每一行时将其移动到循环中,你应该没问题。
答案 2 :(得分:0)
你可以创建一个2维ArrayList<ArrayList<Integer>> recipeContainsID
并为每个食谱保留一个列表,你将它们全部添加到同一个ArrayList中......你必须小心为每个新配方初始化一个新的ArrayList。
答案 3 :(得分:0)
你应该注意while循环中的赋值。除recipeContainsID
之外的每个变量都会在那里更新。对于基元,您要分配新值,而对于String对象,您要为新创建的实例分配引用(请注意,字符串是不可变的)。
因此,如果要将值添加到单独的列表中,则需要为每个已处理的行创建新列表。如果您不想对这些列表进行任何更改,可以使用Arrays.asList()
方法创建它们。
recipeContainsID = Arrays.asList(clean1);