我有一个类,它应该存储一个问题,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"));
虽然我可以使用迭代器顺序调用它们,但是否有任何方法可以从列表中随机访问这些元素? P.S. 我无法使用 .get(int) 函数。
ArrayList
实现了,但是LinkedList
没有实现。那个接口就是……RandomAccess
。 - Olivier Grégoire