简体   繁体   English

JAVA-无法从另一个数组正确填充多维数组

[英]JAVA - Not able to properly fill multidimentional array from another array

I have a string of values separated by commas that I converted into an array, which I was then going to use to create a 2D array. 我有一个用逗号分隔的值字符串,然后将其转换为数组,然后将其用于创建2D数组。 When creating a loop to add the data from the first array to the 2D array it is repeating the data. 创建循环以将数据从第一个数组添加到2D数组时,它正在重复数据。

The output I'm getting is: 我得到的输出是:

4428,40,401,610,2016,3821,31,347,572,2015,4381,38,341,520,2014,2536,17,193,290,2013,4295,39,371,552,2012,4643,45,343,502,2011,3922,28,312,475,2010,4434,30,350,541,2009,4038,28,341,536,2008,218,1,20,28,2007,46,0,6,15,2006,65,0,9,16,2005,4428,40,401,610,2016,3821,31,347,572,2015,4381,38,341,520,2014,2536,17,193,290,2013,4295,39,371,552,2012,4643,45,343,502,2011,3922,28,312,475,2010,4434,30,350,541,2009,4038,28,341,536,2008,218,1,20,28,2007,46,0,6,15,2006,65,0,9,16,2005,4428,40,401,610,2016,3821,31,347,572,2015,4381,38,341,520,2014,2536,17,193,290,2013,4295,39,371,552,2012,4643,45,343,502,2011,3922,28,312,475,2010,4434,30,350,541,2009,4038,28,341,536,2008,218,1,20,28,2007,46,0,6,15,2006,65,0,9,16,2005,4428,40,401,610,2016,3821,31,347,572,2015,4381,38,341,520,2014,2536,17,193,290,2013,4295,39,371,552,2012,4643,45,343,502,2011,3922,28,312,475,2010,4434,30,350,541,2009,4038,28,341,536,2008,218,1,20,28,2007,46,0,6,15,2006,65,0,9,16,2005,4428,40,401,610,2016,3821,31,347,572,2015,4381,38,341,52 4428,40,401,610,2016,3821,31,347,572,2015,4381,38,341,520,2014,2536,17,193,290,2013,4295,39,371,552,2012,4643,45,343,502,2011,3922,28,312,475,2010,4434,30,350,541,2009,4038, 28,341,536,2008,218,1,20,28,2007,46,0,6,15,2006,65,0,9,16,2005,4428,40,401,610,2016,3821,31,347,572,2015,4381,38,341,520, 2014,2536,17,193,290,2013,4295,39,371,552,2012,4643,45,343,502,2011,3922,28,312,475,2010,4434,30,350,541,2009,4038,28,341,536,2008,218,1,20,28,2007,46, 0,6,15,2006,65,0,9,16,2005,4428,40,401,610,2016,3821,31,347,572,2015,4381,38,341,520,2014,2536,17,193,290,2013,4295,39,371,552,2012,4643, 45,343,502,2011,3922,28,312,475,2010,4434,30,350,541,2009,4038,28,341,536,2008,218,1,20,28,2007,46,0,6,15,2006,65,0,9,16, 2005,4428,40,401,610,2016,3821,31,347,572,2015,4381,38,341,520,2014,2536,17,193,290,2013,4295,39,371,552,2012,4643,45,343,502,2011,3922,28,312,475,2010,4434,30,350,541,2009, 4038,28,341,536,2008,218,1,20,28,2007,46,0,6,15,2006,65,0,9,16,2005,4428,40,401,610,2016,3821,31,347,572,2015,4381, 38,341,52 0,2014,2536,17,193,290,2013,4295,39,371,552,2012,4643,45,343,502,2011,3922,28,312,475,2010,4434,30,350,541,2009,4038,28,341,536,2008,218,1,20,28,2007,46,0,6,15,2006,65,0,9,16,2005 0,2014,2536,17,193,290,2013,4295,39,371,552,2012,4643,45,343,502,2011,3922,28,312,475,2010,4434,30,350,541,2009,4038,28,341,536,2008,218,1,20,28,2007, 46,0,6,15,2006,65,0,9,16,2005

and the correct output should be: 正确的输出应为:

4428,40,401,610,2016,3821,31,347,572,2015,4381,38,341,520,2014,2536,17,193,290,2013,4295,39,371,552,2012,4643,45,343,502,2011,3922,28,312,475,2010,4434,30,350,541,2009,4038,28,341,536,2008,218,1,20,28,2007,46,0,6,15,2006,65,0,9,16,2005 4428,40,401,610,2016,3821,31,347,572,2015,4381,38,341,520,2014,2536,17,193,290,2013,4295,39,371,552,2012,4643,45,343,502,2011,3922,28,312,475,2010,4434,30,350,541,2009,4038, 28,341,536,2008,218,1,20,28,2007,46,0,6,15,2006,65,0,9,16,2005

Expected results: 预期成绩:

{{4428,40,401,610,2016} {{4428,40,401,610,2016}

{3821,31,347,572,2015} {3821,31,347,572,2015}

{4381,38,341,520,2014} {4381,38,341,520,2014}

...} and so on, every 5 ...}等等,每5个

My code for adding the array to the 2D array is below: 我的将数组添加到2D数组的代码如下:

 {String[] columns = {"Yards","Touchdowns","Attempts","Incompletions","Year"};



String[] data1 = results1.split(",");

Object [][] data11 = new Object[columns.length][data1.length];
for(int i = 0; i<columns.length;i++){
    for(int j = 0; j<data1.length;j++){
    data11[i][j] = data1[j];
    //System.out.print(data11[i][j]+",");
}
}}

EDIT: Solution! 编辑:解决方案!

Object [][] data11 = new Object[data1.length/columns.length][columns.length];
int column = -1;
for(int j = 0; j<data1.length;j++){
    if(j % columns.length == 0) column = column+1;
            data11[column][j % 5] = data1[j];
}

You also loop the first array (i), so he will repeat it. 您还循环了第一个数组(i),因此他将重复它。 Just write this: 只需写下:

 data11[0][j] = data1[j];

Or even don't do the first loop at all, depending on what you need. 甚至根本不执行第一个循环,具体取决于您的需求。 Assuming your output, you only want to fill in the first position (0) of the array. 假设您的输出,您只想填写数组的第一个位置(0)。

Maybe this work for you: 也许这为您工作:

Object [][] data11 = new Object[columns.length][data1.length / 5];
int column = -1;
for(int j = 0; j<data1.length;j++){
    if(j % 5 == 0) column = column + 1
    data11[j % 5][column] = data1[j];
}

Note the matrix size changed and the assignation too. 注意矩阵大小已更改,分配也已更改。 Haven't tried, probably you can make a prettier version. 尚未尝试过,也许您可​​以制作一个更漂亮的版本。 Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM