How to scramble the rows of a two-dimensional array in Java? This is a problem that many developers often encounter. Shuffling the rows of a two-dimensional array can be achieved by using the shuffle() method in the Collections class. This method can randomly shuffle the order of elements in the collection. First, we need to convert the two-dimensional array into List form, and then use the shuffle() method to randomly sort the list. Finally, converting the list back into a two-dimensional array completes the row shuffling. This method is simple and easy to implement, can effectively scramble the rows of two-dimensional arrays, and provides developers with a more flexible way to process data.
Question content
I wrote some code to read the csv file and do some data preprocessing, then it should shuffle the rows of the 2d array instead of the columns, although the order is not Change.
The problem I'm having is that it does scramble the rows and columns of the 2d array, but I don't know how to fix it.
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(); } }
There are other methods, as you can see, but only this shuffle2darray() method will work fine.
So that I can randomly split the data into 10 parts so that I can do 10 parts cross validation
I did try putting print statements into the code and tried checking the 2d array before and after trying to shuffle, and then checking the csv file to make sure the rows were in the correct order.
This is a small example before filtering:
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 |
After filtering and shuffling the columns:
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 |
Solution
Your code snippet fails to contain a key element: the array you want to sort.
So, a wild shot in the dark, I guess it looks like this:
int[][] allfeatures = ....;
To shuffle, it's a liner. You can discard all code you own:
collections.shuffle(arrays.aslist(allfeatures));
So, why does this work?
Because there is no such thing as a two-dimensional array. java doesn't have them at all.
The int[][] allfeatures
you have is not a 2D array. It is a one-dimensional array whose components are of type int array. So arrays.aslist(allfeatures)
is great because you have an array of objects (not primitives) - each object is an array of int . The expression is of type list<int></int>
.
arrays.aslist
Returns a list still backed by the underlying array. Therefore, .set()
works, but .add()
does not (because an array can change its elements, but cannot grow or shrink). collections.shuffle
can shuffle any list, and only use .set
and .get
to do this: arrays.aslist
supported The operation is very good.
The upshot of all this is that this will rescale allfeatures
itself.
If you want to keep allfeatures
unchanged, please clone first:
int[][] clone = allFeatures.clone(); Collections.shuffle(Arrays.asList(clone));
The above is the detailed content of How to shuffle rows of 2D array in Java?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
