我有一個應該存儲問題的類,4個答案和1個正確的答案。隨機訪問Java中的鏈接列表
public class questionconstructor {
String ques;
String opt1;
String opt2;
String opt3;
String opt4;
String ans;
questionconstructor(String q,String o1,String o2,String o3,String o4,String an)
{
ques=q;
opt1=o1;
opt2=o2;
opt3=o3;
opt4=o4;
ans=an;
}
}
從主類我使用了一個鏈表中添加元素的類。
LinkedList<questionconstructor> qset = new LinkedList<questionconstructor>();
qset.add(new questionconstructor("What is the fastest animal in the world?","Falcon","Cheetah","Fly","Puma","Cheetah"));
qset.add(new questionconstructor("What is the slowest animal in the world?","Tortoise","Turtle","Sloth","Crocodile","Tortoise"));
qset.add(new questionconstructor("What is the largest animal in the world?","Girrafe","Elephant","Whale","Mammoth","Whale"));
qset.add(new questionconstructor("What is the fastest car in the world?","Bugatti Veyron","Ferrari Enzo","SSC Ultimate Aero","Aston Martin DB7","Bugatti Veyron"));
qset.add(new questionconstructor("Which is of these buildings has a replica in Las Vegas?","Taj Mahal","Great Wall of China","Big Ben","Eiffel Tower","Eiffel Tower"));
雖然我能夠順序調用這些使用迭代器,但有什麼辦法隨機訪問從列表中這些元素? P.S.我無法使用.get(int)函數。
不要使用用於隨機訪問的列表。改爲使用數組類型的容器。 – Bathsheba
鏈表的定義並不意味着這樣的工作。見維基百科:https://en.wikipedia.org/wiki/Linked_list – Chris
有一個接口,'ArrayList'工具,但'LinkedList'沒有。該接口是...'RandomAccess'。 –