简体   繁体   English

使用嵌套的for循环将1d数组转换为2d数组

[英]Convert 1d array into 2d array using nested for loops

Im trying to scan a file and create a 1D array for each line, then immediately following the creation of that array copy into the first row of a 2D array. 我试图扫描文件并为每行创建一个1D数组,然后在创建该数组后立即将其复制到2D数组的第一行中。 I've gotten my code to work for only the first row of the file. 我已经将代码仅用于文件的第一行。 It won't move onto the next line. 它不会移动到下一行。 it copies the same line as it traverses the whole 2d array. 它遍历整个2d数组时复制同一条线。 i know that happens because the counter for the scanning the next line doesn't increase before it reaches the end of the 2d array. 我知道发生这种情况是因为扫描下一行的计数器在到达2d数组的末尾之前不会增加。 how can i increase the counter for scanning the next line? 如何增加计数器以扫描下一行? heres my code: (tempString is the 1D array already created before this loop) 这是我的代码:(tempString是此循环之前已经创建的一维数组)

    for(int i = 0; i < 7; i++){
        tempString = scnr.nextLine().split(" ");
        //add lines to 2d array
        for(int r = 0; r < 7; r++){
            int x = 0; //moves along each element in tempString
            for(int c = 0; c < tempString.length; c++){
                temp[r][c] = Double.parseDouble(tempString[x]);
                x++;
            }
        }
    }

You have 3 loops, but you only need two. 您有3个循环,但只需要两个循环。 Each input row read from the file will become a row of the 2-D array : 从文件读取的每个输入行将成为二维数组的一行:

    for(int r = 0; r < 7; r++){
        tempString = scnr.nextLine().split(" ");
        temp[r] = new double[tempString.length];
        for(int c = 0; c < tempString.length; c++){
            temp[r][c] = Double.parseDouble(tempString[c]);
        }
    }

I am not quite sure with what exactly you need. 我不确定您到底需要什么。 But what I have derived from the question is that you have a file and you need to split it in a 2D array, in which the columns should contain the individual items within each line and each row should be in a new line. 但是我从这个问题得出的结论是,您有一个文件,需要将其拆分为2D数组,其中各列应在每一行中包含各个项目,而每一行应在新行中。

My recommendation is to use ArrayList which will handle the dynamic lengths for you. 我的建议是使用ArrayList ,它将为您处理动态长度。

See the below example: 请参见以下示例:

Say I have a file 'text.txt' which contains some data like this 说我有一个文件'text.txt',其中包含一些数据,例如

abc def ghi
jhl mnop qrs uv
wx yz

Then here is a program which forms a 2D array, first dimension containing each row and the second dimension containing tokens from each line. 然后是一个程序,该程序形成一个2D数组,第一维包含每一行,第二维包含来自每一行的标记。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Split {
public static void main(String args[]){
    ArrayList<ArrayList<String>> columns = new ArrayList<ArrayList<String>>();

    columns.add(new ArrayList<String>());//Col 1
    columns.add(new ArrayList<String>());//Col 2
    columns.add(new ArrayList<String>());//Col 3

BufferedReader br = null;
try
  {
    br = new BufferedReader(new FileReader("src\\text.txt"));
    String sCurrentLine;
    int j=0;
    while ((sCurrentLine = br.readLine()) != null) {
    String sLine[] = sCurrentLine .split(" ");
    for(int i = 0; i < sLine.length; i++)
    {
        columns.get(j).add(sLine[i]);
    }
    j++;
    }
    for(ArrayList<String> line:columns){
        for(String tokens:line)
            System.out.println(tokens);
        System.out.println();
    }
  } catch (IOException ex) {
       ex.printStackTrace();
  }
}
}

If this is not what you need, please try to elaborate your question further if possible with examples. 如果这不是您所需要的,请尝试通过示例进一步阐述您的问题。

Please note that I'm using spaces (" ") to split the tokens, you can replace it by anything that you are using. 请注意,我使用空格(“”)分割令牌,您可以将其替换为使用的任何内容。

I hope it helps :) 希望对您有所帮助:)

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

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