动态填充2D数组-Java
我可以在Java中顺序检索以下元素列表,并且需要将它们插入AxB维度矩阵中。如何将这些元素按其顺序放入矩阵动态填充2D数组-Java,java,arrays,Java,Arrays,我可以在Java中顺序检索以下元素列表,并且需要将它们插入AxB维度矩阵中。如何将这些元素按其顺序放入矩阵 Element: rainy Element: hot Element: high Element: false Element: No Element: rainy Element: cold Element: normal Element: true Element: yes 我想要的输出是: array = [[rainy, hot, high, false, No],[rainy
Element: rainy
Element: hot
Element: high
Element: false
Element: No
Element: rainy
Element: cold
Element: normal
Element: true
Element: yes
我想要的输出是:
array = [[rainy, hot, high, false, No],[rainy, cold, normal, true, yes]]
如何开始?这个问题对我来说不是很清楚,但由于我还不能发表评论,这里有一个答案: 我将假设您的数据位于一个数组中(如so
String[]dataset
),并希望将其转换为二维数组,因此以下是步骤
首先使用AxB大小初始化二维数组:
int a = dataset.length/5; // the size of your dataset divided by the chuncks
int b = 5; // the size of your chunck
String[][] processedDataset = new String[a][b];
然后,您希望用数据集填充二维数组。这可以通过基本for循环完成:
int k = 0;
for(int i = 0; i < processedDataset.length; i++){
for(int j = 0; j < processedDataset[i].length; j++){
processedDataset[i][j] = dataset[k++];
}
}
intk=0;
for(int i=0;i
您可以使用guava库进行分区
列表元素=/。。。
List smallerlist=Lists.partition(元素,#您想要的分区);
您至少尝试过解决这个问题吗?向我们展示一些您尝试过的代码。查看java中的列表分区。查看此帖子:但我建议您创建一个包含相关数据的类。例如,您可以创建一个类Weather
或其他类似的类,其中包含降水量,温度,等等。数据集[i+j]
应该是数据集[i*处理数据集[i]。长度+j]
。另一个选项是使用第三个循环变量k
,在循环的外部中初始化为0,在循环的内部中递增,然后访问数据集[k],谢谢@SirRaffleBuffle,相应地编辑了答案。
List<String> element= //...
List<List<String>> smallerLists = Lists.partition(element, # of partition you want);