我需要將CSV文件的每一行轉換爲分離的堆棧(每行一個堆棧)。我的問題是在我掃描每行並將CSV轉換爲堆棧之後,我應該在哪裏存儲堆棧?我不知道我要創建多少堆棧,所以我需要一個動態數據結構來存儲創建的堆棧。以下是我的代碼。我可以將堆棧添加到ArrayList(或鏈接列表)嗎?
Scanner scanner = new Scanner(new FileReader(filepath));
Stack<String> st = new Stack<String>();
String line;
while (scanner.hasNextLine()){
line = scanner.nextLine();
List<String> items = Arrays.asList(line.split(":",-1));
String x=items.get(0);
while(x == null || x.equals("#"))
{
line = scanner.nextLine();
items=Arrays.asList(line.split(":",-1));
x=items.get(0);
}`
st.addAll(items);
//i need to store st into a data structure before i clear it.
st = new Stack<String>();
}
你問是選擇一個'ArrayList'還是'LinkedList'?或者你問一般來說是否可以有一個堆棧列表? – RealSkeptic
你爲什麼不試試?是什麼讓你覺得你不能將堆棧存儲到列表中? –
'List> stackList = new ArrayList >()'是你似乎在尋找的東西。 –
ritratt