Java 作为arraylist/hashmap从文本文件读取
好的,下面是我要写的解释:Java 作为arraylist/hashmap从文本文件读取,java,file-io,arraylist,hashmap,Java,File Io,Arraylist,Hashmap,好的,下面是我要写的解释: getBreadInfo-将bread.txt读入数组列表(包含bread) 名称、$和价格),然后分配给数组breadInfo[],然后 将此数组返回给SandwichApp以显示面包菜单 getBread-与getBreadInfo类似,只是它只包含 面包名称,并返回另一个数组面包[],以便SandwichApp绘制 用户选择的面包是因为用户键入了一个数字 关联面包(索引+1),而不是面包名称 getMapBreadPrice-与上述两个类似,只是它返回一个 包
-将bread.txt读入数组列表(包含bread) 名称、$和价格),然后分配给数组breadInfo[],然后 将此数组返回给SandwichApp以显示面包菜单getBreadInfo
-与getBreadInfo类似,只是它只包含 面包名称,并返回另一个数组面包[],以便SandwichApp绘制 用户选择的面包是因为用户键入了一个数字 关联面包(索引+1),而不是面包名称getBread
-与上述两个类似,只是它返回一个 包含面包名称(键)和价格(值)对值的哈希映射 让SandwichApp计算出面包用户的价格 选中getMapBreadPrice
public class SandwichDB {
private ArrayList<String> breadsList = null;
public String[] getBreadInfo()
{
breadsList = new ArrayList<>();
try (BufferedReader in =
new BufferedReader(
new FileReader("bread.txt")))
{
String line = in.readLine();
while (line != null)
{
String[] elems = line.split("~");
breadsList.add(elems[0]+ " $" + elems[1]);
}
}
catch(IOException e)
{
System.out.println(e);
return null;
}
String[] breadInfo = breadsList.toArray(new String[]{});
return breadInfo;
}
public String[] getBread()
{
breadsList = new ArrayList<>();
try (BufferedReader in =
new BufferedReader(
new FileReader("bread.txt")))
{
String line = in.readLine();
while (line != null)
{
String[] elems = line.split("~");
breadsList.add(elems[0]);
}
}
catch(IOException e)
{
System.out.println(e);
return null;
}
String[] bread = breadsList.toArray(new String[]{});
return bread;
}
public HashMap<String, String> getMapBreadPrice()
{
HashMap<String, String> mapBreadPrice = new HashMap<>();
String line, elems[];
try
{
FileReader fr = new FileReader("bread.txt");
BufferedReader br = new BufferedReader(fr);
while ((line=br.readLine()) != null)
{
elems = line.split("~");
mapBreadPrice.put(elems[0], elems[1]);
}
}
catch(IOException e)
{
System.out.println(e);
return null;
}
return mapBreadPrice;
}
}
公共类三明治{
private ArrayList breadsList=null;
公共字符串[]getBreadInfo()
{
breadlist=newarraylist();
尝试(在中使用BufferedReader)=
新的缓冲读取器(
新的文件阅读器(“bread.txt”))
{
String line=in.readLine();
while(行!=null)
{
String[]elems=line.split(“~”);
添加(元素[0]+“$”+元素[1]);
}
}
捕获(IOE异常)
{
系统输出打印ln(e);
返回null;
}
String[]breadInfo=breadList.toArray(新字符串[]{});
返回breadInfo;
}
公共字符串[]getBread()
{
breadlist=newarraylist();
尝试(在中使用BufferedReader)=
新的缓冲读取器(
新的文件阅读器(“bread.txt”))
{
String line=in.readLine();
while(行!=null)
{
String[]elems=line.split(“~”);
添加(元素[0]);
}
}
捕获(IOE异常)
{
系统输出打印ln(e);
返回null;
}
String[]bread=breadList.toArray(新字符串[]{});
归还面包;
}
公共HashMap getMapBreadPrice()
{
HashMap mapBreadPrice=新HashMap();
字符串行,元素[];
尝试
{
FileReader fr=新的FileReader(“bread.txt”);
BufferedReader br=新的BufferedReader(fr);
而((line=br.readLine())!=null)
{
元素=行分割(“~”);
mapBreadPrice.put(元素[0],元素[1]);
}
}
捕获(IOE异常)
{
系统输出打印ln(e);
返回null;
}
返回mapBreadPrice;
}
}
第一条读线
位于之前,而因此不会重复。因此,这段时间并没有结束
for (;;) {
String line = in.readLine();
if (line == null) {
break;
}
为了构建3个结构,您似乎正在读取同一个文件3次。您应该通过一次读取文件来构建数据结构。它是否符合您的预期?