【发布时间】:2017-06-27 15:15:00
【问题描述】:
我有一个类应该存储一个问题、4 个答案和 1 个正确答案。
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"));
虽然我可以使用迭代器顺序调用这些元素,但有没有办法从列表中随机访问这些元素? 附言我无法使用 .get(int) 函数。
【问题讨论】:
-
不要使用列表进行随机访问。请改用数组类型的容器。
-
根据定义,链接列表并不意味着这样工作。参见维基百科:en.wikipedia.org/wiki/Linked_list
-
ArrayList实现了一个接口,但LinkedList没有实现。那个界面是……RandomAccess.
标签: java linked-list random-access