首页  >  文章  >  Java 如何打乱二维数组的行?

Java 如何打乱二维数组的行?

PHPz
PHPz转载
2024-02-09 16:15:20866浏览

Java中如何打乱二维数组的行?这是许多开发者经常遇到的问题。打乱二维数组的行可以通过使用Collections类中的shuffle()方法来实现。该方法可以随机打乱集合中的元素顺序。首先,我们需要将二维数组转换为List形式,然后再使用shuffle()方法对列表进行随机排序。最后,将列表转换回二维数组即可完成行的打乱。这种方法简单易行,能够有效地打乱二维数组的行,为开发者提供了更灵活的数据处理方式。

问题内容

我编写了一些代码来读取 csv 文件并进行一些数据预处理,然后它应该对 2d 数组的行而不是列进行洗牌,尽管其顺序无法更改。

我遇到的问题是它确实会打乱 2d 数组的行和列,但我不知道如何修复它。

import java.io.*;
import java.nio.file.paths;
import java.util.*;

public class randomforest
{
  public static void shuffle2darray()
    {
        int numrows = allfeatures.length;
        list<integer> indices = new arraylist<>();

        // populate the list with indices
        for (int i = 0; i < numrows; i++) {
            indices.add(i);
        }

        // shuffle the list of indices
        collections.shuffle(indices);

        // create a copy of the original array
        int[][] shuffledarray = new int[numrows][allfeatures[0].length];

        // use shuffled indices to rearrange the rows of the original array
        for (int i = 0; i < numrows; i++) {
            shuffledarray[i] = allfeatures[indices.get(i)];
        }

        // update the original array with the shuffled values
        allfeatures = shuffledarray;
    }

    public static void main(string[] args) throws ioexception
    {
        randomforest rf = new randomforest("se-dermatitis dataset.csv");

        rf.getdatarows();
        rf.markfilters();
        rf.removeattributes();

        int count = 1;
        int counter = 1;

        int totalpredacc = 0;

        int allfeaturessize = allfeatures.length;
        int foldsize = allfeaturessize / 10;

        double[] threshold = {0.4, 0.5, 0.6, 0.7};

        shuffle2darray();
    }
}

还有其他方法,如您所见,但仅此 shuffle2darray() 方法就可以正常工作。

这样我就可以将数据随机分为 10 份,以便我可以进行 10 份交叉验证

我确实尝试将 print 语句放入代码中,并尝试在尝试洗牌之前和之后检查 2d 数组,然后检查 csv 文件以确保该行的顺序正确。

这是过滤前的小示例:

cpi_9606.ensp00000000233 cpi_9606.ensp00000000412 cpi_9606.ensp00000000442 cpi_9606.ensp00000001008 cpi_9606.ensp00000001146 cpi_9606.ensp00000002165 cpi_9606.ensp00000002829 cpi_9606.ensp00000003084 cpi_9606.ensp00000003100
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 180

过滤和打乱列后:

cpi_9606.ENSP00000000442 cpi_9606.ENSP00000001008 cpi_9606.ENSP00000003084 cpi_9606.ENSP00000003100 cpi_9606.ENSP00000005178 cpi_9606.ENSP00000011292 cpi_9606.ENSP00000011653 cpi_9606.ENSP00000012443 cpi_9606.ENSP00000013034 cpi_9606.ENSP00000014930
0 900 0 0 0 900 0 0 0
900 0 0 0 0 0 928 900 338
322 236 0 0 0 0 0 0 0

解决方法

您的代码片段未能包含关键元素:您想要排序的数组。

所以,在黑暗中进行一次疯狂的尝试,我猜它看起来像这样:

int[][] allfeatures = ....;

要洗牌,它是一个衬垫。您可以放弃您拥有的所有代码:

collections.shuffle(arrays.aslist(allfeatures));

那么,为什么这会起作用?

因为不存在二维数组这样的东西。 java 根本没有它们。

您拥有的 int[][] allfeatures 不是二维数组。它是一个一维数组,其组件类型为 int 数组。因此, arrays.aslist(allfeatures) 非常好,因为您有一个对象数组(不是基元) - 每个对象都是一个 int 数组。该表达式的类型为 listdfcfb097077124ccf9617875ee28bb24

arrays.aslist 返回一个仍然由底层数组支持的列表。因此, .set() 有效,但 .add() 无效(因为数组可以更改其元素,但不能增长或缩小)。 collections.shuffle 可以打乱任何列表,并且仅使用 .set.get 来执行此操作: arrays.aslist 支持的操作就很好。

这一切的结果是,这将重新调整 allfeatures 本身。

如果你想保持allfeatures不变,请先克隆:

int[][] clone = allFeatures.clone();
Collections.shuffle(Arrays.asList(clone));

以上是Java 如何打乱二维数组的行?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除