English 中文(简体)
如何从 Java的一组阵列线读?
原标题:How to read from set of arrays line by line in Java?

我的法典如下:

private static ArrayList<String[]> file;

...

void method()
{
  file = new ArrayList<String[]>();
  ...
  String[] s = str.split(";");
  file.add(s);
}

以上条目:<条码> 半殖民者抓住了这一优势。

我的问题是,我想通过<代码>ArrayList中的每一个阵列,并从各个阵列中获得一个内容。

例如,如果一个阵列为hello;现在为“<>/code>,另一个阵列为how;你为“<>/code>,那么我就希望将其检索为'hello,今天是“(即每个阵列的一个要素)。

我不能想办法这样做。

任何帮助都将受到高度赞赏。

提前感谢。

问题回答
int currentIndex = 0;
StringBuilder b = new StringBuilder();
boolean arraysHaveMoreElements = true;
while (arraysHaveMoreElements) {
    arraysHaveMoreElements = false;
    for (String[] array : file) {
        if (array.length > currentIndex) {
            b.append(array[currentIndex];
            arraysHaveMoreElements = true;
        }
    }
    currentIndex++;
}

这并非太困难——假设各行各有同等规模!

private void dumpColumnwise() {

  // generate test input
  // String[] firstRow = {"a00", "a01", "a02"};
  // String[] secondRow = {"a10", "a11", "a12"};
  String[] firstRow = "a00;a01;a02".split(";");
  String[] secondRow = "a10;a11;a12".split(";");

  List<String[]> rows = new ArrayList<String[]>();
  rows.add(firstRow);
  rows.add(secondRow);

  // get longest row
  int numberOfColumns = 0;
  for (String[] row:rows) {
    numberOfColumns = Math.max(numberOfColumns, row.length);
  }

  int numberOfRows = rows.size();
  for (int column = 0; column < numberOfColumns; colunm++) {
    for (int row = 0; row < numberOfRows; row++) {
      String[] row = rows.get(row);
      if (column < row.length) {
        System.out.printf(" %s", rows.get(row)[column]);
      }
    } 
  }
}

Room for 改进:允许输入数据,长度各异。 在这种情况下,如果一栏指数对实际增长有效,则须(1)确定最长的行刑和(2)测试。

to read an array list we can use iterator interface.` using iterator we can go through the array list. this is just an example code.

public static void  main(String []args)
   {
      ArrayList<String[]>  list=new ArrayList<>();
      String arr1[]={"hello","are","today"};
        String arr2[]={"how","you"};
        list.add(arr1);
        list.add(arr2);
       Iterator<String[]> it = list.iterator();
         while(it.hasNext())
         {
             String[] temp=it.next();
             System.out.print(temp[0]+" ");

         }



   }`

产出=如何计算;





相关问题
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 ...