【问题标题】:Randomly access a linked list in Java [duplicate]随机访问Java中的链表[重复]
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 这真的很有帮助。谢谢。
  • LinkedList 实现了 List 接口,该接口定义了 get(int) 函数。因此,无需使用带有迭代器的自己的实现来访问列表中的元素。
  • 嗯,你是对的。但我假设它在内部使用迭代器,所以你也可以使用它。然而,这仍然是非常不好用的,所以我推荐一个 ArrayList 来直接访问列表中的随机位置。
【解决方案2】:

对于随机访问 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() 也会给你数组列表的大小。

【讨论】:

    【解决方案3】:

    你最好使用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 作为类的名称不是一个好的选择

    【讨论】:

    • Random 和这个有什么关系?问题是关于随机访问而不是顺序访问,而不是选择随机值。
    【解决方案4】:
    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-27
      • 2015-08-01
      • 1970-01-01
      • 2010-09-17
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      相关资源
      最近更新 更多