English 中文(简体)
Java: 从文件导入数组到数组
原标题:Java: Import array from file to array
  • 时间:2012-05-25 10:53:08
  •  标签:
  • java
  • arrays

I have files which contains array s like this: Here is a snippet from one of my .txt

itemlist= new int[][] {
    { 0, 0, 0, 0, 0}, 
    { 1, 1, 1, 1, 1}, 
    { 2, 2, 2, 2, 2}, 
    { 3, 3, 3, 3, 3}, 
    { 4, 4, 4, 4, 4}
};

您可以看到. txt 文件包含一个多维数组列表。 是否有办法将它转换成 Java 内的数组?

干杯! 干杯! 干杯! 干杯!

最佳回答

我觉得这是家庭作业 所以我不能给你密码 但如果我是你 我会:

1 1) 将文件文本放入一个字符串( 没有新行标记)

2) (我假设 one file = = = = = = = 表格数据 ) 尝试在第一个 和最后一个 之间分离文本(再次regex可以帮助)

3) 从第2点分割内容, 以便从每个 中分别获取数据( 使用分割和再次regex)

(4) 4) 通过计数 和计数 来估计数组的大小(通过regex或迭代字符)

5),现在我可以创建表格(我知道大小)

第3点第6点,我隔开所有行(或科,取决于你如何将其放入表格),所以现在是时候阅读并放入表格了,所以我们需要通过第3点的所有数据进行循环,以及:

6.1) 括号

6.2) 使用 , 拆分

6.3) 最小空格

6.4) 向整数分析,然后放入数组

希望这足以创造代码

问题回答
public static void main(String[] args) { 

    int itemlist[][]= new int[][] {
      { 0, 0, 0, 0, 0}, 
      { 1, 1, 1, 1, 1}, 
      { 2, 2, 2, 2, 2}, 
      { 3, 3, 3, 3, 3}, 
      { 4, 4, 4, 4, 4}
    };

    int length=itemlist.clone().length*itemlist.length;
    int tab[]=new int[length];
    int i=1;
    for(int j=0;j<itemlist.length;j++) 
    {
      for(int k=0;k<itemlist.clone().length;k++)
      {
         tab[i]=itemlist[j][k];
         i++;

         if (i== length) break ;
      }
    }

    for(int h=0;h<length;h++){
      System.out.println(" "+tab[h]);
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...