📜  如何在Java中打乱 LinkedList 中的元素?

📅  最后修改于: 2022-05-13 01:54:25.520000             🧑  作者: Mango

如何在Java中打乱 LinkedList 中的元素?

LinkedList 是存在于Java.util 包中的 Collection 框架的一部分。这个类是 LinkedList 数据结构的一个实现,它是一种线性数据结构,其中元素不存储在连续的位置,每个元素都是一个单独的对象,具有数据部分和地址部分。这些元素使用指针和地址链接。每个元素称为一个节点。给定一个 LinkedList,任务是对 LinkedList 进行洗牌。这些是解决问题的方法。

方法一(使用自定义方法)

  • 创建一个链表。
  • 通过toArray()方法将其元素存储在数组中。
  • 打乱数组元素。
  • LinkedList上使用ListIterator ,通过next()方法遍历LinkedList,同时通过set()方法将Array的shuffle后的数据存入List。

例子

Java
import java.util.*;
public class GFG {
  
    public static void main(String args[])
    {
  
        // creating an instance of LinkedList
        LinkedList list = new LinkedList<>();
  
        // adding data to the LinkedList
        list.add(45);
        list.add("GFG");
        list.add(2.56f);
        list.add(965.321);
        list.add('A');
        list.add(true);
  
        System.out.println("The list before shuffle : "
                           + list.toString());
        System.out.println();
  
        // creating an Array and storing all data of the
        // list to the array
        Object[] array = list.toArray();
  
        // here we are shuffling more than once
        // shuffle 1
        arrayShuffle(array);
        listDataAdder(array, list);
        System.out.println("The list after shuffle 1 : "
                           + list.toString());
        System.out.println();
  
        // shuffle 2
        arrayShuffle(array);
        listDataAdder(array, list);
        System.out.println("The list after shuffle 2 : "
                           + list.toString());
        System.out.println();
  
        // shuffle 3
        arrayShuffle(array);
        listDataAdder(array, list);
        System.out.println("The list after shuffle 3 : "
                           + list.toString());
    }
  
    // Creating a data to add shuffled data of the array to
    // the list
    static void listDataAdder(Object[] arr,
                              LinkedList list)
    {
        // creating a ListIterator on the LinkedList
        ListIterator it = list.listIterator();
  
        // Traversing the LinkedList and setting all
        // shuffled data of the array to the list
        for (Object e : arr) {
            it.next();
            it.set(e);
        }
    }
  
    // creating a method to shuffle the array
    static void arrayShuffle(Object[] arr)
    {
        Random rand = new Random();
        for (int i = 0; i < arr.length - 1; i++) {
            
            // select index randomly
            int index = rand.nextInt(i + 1);
            
            // swapping between i th term and the index th
            // term
            Object g = arr[index];
            arr[index] = arr[i];
            arr[i] = g;
        }
    }
}

Java
import java.util.Collections;
import java.util.LinkedList;
  
public class GFG {
    public static void main(String args[])
    {
        // Creating a LinkedList
        LinkedList list = new LinkedList<>();
        list.add(45);
        list.add("GFG");
        list.add(2.56f);
        list.add(965.321);
        list.add('A');
        list.add(true);
  
        System.out.println(
            "The LinkedList before shuffle : "
            + list.toString());
        System.out.println();
  
        // here we are shuffling more than once
        // shuffle 1
        Collections.shuffle(list);
        System.out.println(
            "The LinkedList after shuffle 1 : "
            + list.toString());
        System.out.println();
  
        // shuffle 2
        Collections.shuffle(list);
        System.out.println(
            "The LinkedList after shuffle 2 : "
            + list.toString());
        System.out.println();
  
        // shuffle 3
        Collections.shuffle(list);
        System.out.println(
            "The LinkedList after shuffle 3 : "
            + list.toString());
    }
}

输出
The list before shuffle : [45, GFG, 2.56, 965.321, A, true]

The list after shuffle 1 : [965.321, 2.56, GFG, 45, A, true]

The list after shuffle 2 : [965.321, 45, A, 2.56, GFG, true]

The list after shuffle 3 : [965.321, 45, GFG, 2.56, A, true]

方法 2(使用Collections.shuffle(list)

  • 创建一个链表。
  • 使用Collections类的shuffle()方法对列表的元素进行混洗。

宣言

public static void shuffle(List list)

句法

Collections.shuffle(list);

异常:如果给定的列表或其列表迭代器不支持设置操作,则此方法抛出UnsupportedOperationException

例子:

Java

import java.util.Collections;
import java.util.LinkedList;
  
public class GFG {
    public static void main(String args[])
    {
        // Creating a LinkedList
        LinkedList list = new LinkedList<>();
        list.add(45);
        list.add("GFG");
        list.add(2.56f);
        list.add(965.321);
        list.add('A');
        list.add(true);
  
        System.out.println(
            "The LinkedList before shuffle : "
            + list.toString());
        System.out.println();
  
        // here we are shuffling more than once
        // shuffle 1
        Collections.shuffle(list);
        System.out.println(
            "The LinkedList after shuffle 1 : "
            + list.toString());
        System.out.println();
  
        // shuffle 2
        Collections.shuffle(list);
        System.out.println(
            "The LinkedList after shuffle 2 : "
            + list.toString());
        System.out.println();
  
        // shuffle 3
        Collections.shuffle(list);
        System.out.println(
            "The LinkedList after shuffle 3 : "
            + list.toString());
    }
}
输出
The LinkedList before shuffle : [45, GFG, 2.56, 965.321, A, true]

The LinkedList after shuffle 1 : [965.321, 2.56, true, 45, GFG, A]

The LinkedList after shuffle 2 : [2.56, A, true, 45, GFG, 965.321]

The LinkedList after shuffle 3 : [GFG, A, 965.321, 45, true, 2.56]