在Java中随机访问一个链表

Randomly access a linked list in Java

我有一个 class 应该存储一个问题、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;
}

}

从主 class 我使用链表向 class 添加元素。

   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) 函数。

根据定义,LinkedList 中的随机访问没有意义,因为每个元素仅链接到它的直接邻居。您必须自己使用迭代器和随机数实现它,或者您可以改用 ArrayList(参见 here 示例)。

对于随机访问,没有创建 LinkedList,最好使用基于数组的方法来为您提供随机访问。

questionconstructor qset[] = new questionconstructor[size];

    qset[0] = new questionconstructor("What is the fastest animal in the world?","Falcon","Cheetah","Fly","Puma","Cheetah");
    qset[1] = new questionconstructor("What is the slowest animal in the world?","Tortoise","Turtle","Sloth","Crocodile","Tortoise");

//and many more items in you array like above

然后你可以访问任何问题作为 qset[index],知道它的 index

或者您可以使用 ArrayList 而不是 Arrayadvantages of ArrayList over Array

    ArrayList<questionconstructor> qset = new ArrayList<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"));

//and many more items in you array like above

qset.get(index) 将用于 arraylist 中由 index 表示的任何对象。另外 qset.size() 会给你数组列表的大小。

你最好使用 ArrayList.

List<questionconstructor> qset = new ArrayList<questionconstructor>();

您可以使用 qset.size() 来获取列表中元素的数量。然后随机访问其中之一。像这样:

int amount = qset.size();
Random rand = new Random();
int randomNumber = rand.nextInt(amount);
questionconstructor randomQuestion = qset.get(randomNumber);

您必须为此导入 java.util.Random

顺便说一句:qset 作为列表的名称和 questionconstructor 作为 class 的名称不是一个好的选择

is there any way to randomly access these elements from the list?
P.S. I am unable to use a .get(int) function.

我理解你问是否可以在常数时间内访问链表中的元素。

这不是链表的设计目的,它旨在允许在恒定时间内在任何位置删除或插入元素。权衡是在任何位置选择元素不是常数,而是线性时间。

这里是LinkedList的相关实现代码:

    if (index < (size >> 1)) {
        Node<E> x = first;
        for (int i = 0; i < index; i++)
            x = x.next;
        return x;
    } else {
        Node<E> x = last;
        for (int i = size - 1; i > index; i--)
            x = x.prev;
        return x;
    }

列表中的元素是按顺序访问的,而不是随机访问的。

如果你想要随机访问,将链表转换为数组

questionconstructor[] array = qset.toArray(new questionconstructor[qset.size()]);

的 ArrayList
ArrayList<questionconstructor> arrayList = new ArrayList<>(qset);