【问题标题】:Shuffle an Array algorithm - Java随机排列数组算法 - Java
【发布时间】:2016-03-30 08:52:52
【问题描述】:

我正在编写一个程序,该程序旨在读取一个充满链接的 .txt 文件并对数组中的链接进行排序,以便以后可以随机访问这些链接。

这是我所做的(但它不起作用:D):

import java.io.*;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Scan{

        public static void main(String[] args) throws FileNotFoundException{
        String test = "";
        int i = 0;
        int temp = 0;


        Scanner link = new Scanner(new File("links.txt"));
        while(link.hasNextLine()){
            test = link.nextLine();
            temp++;
        }
        link.close();

        String[] links = new String[temp];

        Scanner urls = new Scanner(new File("links.txt"));

        for (i = 0; i < temp; i++)
        {
            test = urls.nextLine();
            links[i] = test;
            System.out.println("Sorted: " + links[i]);
        }
        urls.close();
        System.out.println("Size of LINKS: " + temp);        


                    // - - - - - - - - - - - - - - - - - - - - - //
                    //  Start of the unsorting algorithm         //
                    //  (It's just not working, need a new one)  //
                    // - - - - - - - - - - - - - - - - - - - - - //

        String[] copy = new String [temp];
        int[] used = new int [temp];
        for(i = 0; i < temp; i++)
        {
            for (int k = 0; k < temp; k++)
            {
                    int j = (int) (Math.random() * temp);
                    //System.out.println(j);
                    if (j == used[k])
                    {
                        k--;
                    }
                    else
                    {
                        used[k] = j;
                        copy[j] = links[i];
                    }

            }
            System.out.println(links[i]);            
        }
    }
}

编辑:我当前的未排序算法正在按起始顺序显示所有链接。

【问题讨论】:

  • 你知道List,比如ArrayList吗?
  • @Maljam 我听说过,但我不熟悉。这会让这项工作更容易吗?
  • 是的,您对涉及ArrayList的解决方案还满意吗?
  • @Maljam 当然,那样我会学到一些新东西:D
  • 仅供参考 - 有一个标准的洗牌算法,称为 Fisher-Yates 或 Knuth Shuffle:algs4.cs.princeton.edu/11model/Knuth.java.htmldotnetperls.com/shuffle-java

标签: java arrays algorithm sorting


【解决方案1】:

问题出在第二个循环中,您正在选择一个随机索引j,但您在检查used[k] == j 时是否应该检查Does the array used contain j

所以一个解决方案是创建一个方法boolean contains(int[] arr, int val) 来检查它是否已经存在。或者另一个简单的解决方案:

ArrayList<String> links = //read from file

ArrayList<String> unsorted = new ArrayList<String>();

while(links.size() != 0) {
    unsorted.add( links.remove( (int)(Math.random()*links.size()) ) );
}

如果你想在String[]中输出,你可以使用unsorted.toArray()

【讨论】:

  • 我明白了...我会更改它,看看它是否已修复。谢谢!
  • 我知道他想手动执行此操作,但也许很高兴知道,Collections 有一个 shuffle 方法可以为您执行此操作:Collections.shuffle(list);
【解决方案2】:

为什么要重新发明轮子?

List<String> list = Arrays.asList(links);
Collections.shuffle(list);
String[] copy = list.toArray(new String[temp]);

如果您使用 List 而不是数组,您的代码会简单得多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    相关资源
    最近更新 更多