【问题标题】:Array of Strings into an ArrayList of Arraylist [duplicate]将字符串数组转换为 Arraylist 的 ArrayList [重复]
【发布时间】:2017-04-10 15:46:42
【问题描述】:

我想将字符串数组转换为ArrayList 的ArrayList,其中内部ArrayList 具有动态数量的元素。谁能帮忙?提前致谢

String[] sentences = {"hello","how are you","i am fine","and you ?","thank you"}
//Output with number of elements = 2
["hello","how are you"]
["i am fine","and you ?"]
["thank you"]
//Output with number of elements = 3
["hello","how are you","i am fine"]
["and you ?","thank you"]

【问题讨论】:

  • 什么是差异。在元素数 = 2 和 3 之间?
  • @bibuche 你想要代码吗?
  • 元素的数量必须是动态的。句子总数为 5。元素数 = 2 我们得到 2,2,1。元素数 = 3 我们得到 3,2 作为内部 Arraylist 中的字符串数。

标签: java arrays arraylist multidimensional-array


【解决方案1】:
public static void main(String[] args) {
    String[] sentences = {"hello", "how are you", "i am fine", "and you ?", "thank you"};
    System.out.println(split(2,sentences));
    System.out.println(split(3,sentences));
}

public static List<List<String>> split(int numberOfElements, String[] sentences) {
    List<List<String>> lists = new ArrayList<List<String>>();
    int index = 0;
    for (String sentence : sentences) {
        if (index % numberOfElements == 0) {
            lists.add(new ArrayList<String>());
        }
        lists.get(index / numberOfElements).add(sentences[index]);
        index++;
    }
    return lists;
}

输出:

[[hello, how are you], [i am fine, and you ?], [thank you]]
[[hello, how are you, i am fine], [and you ?, thank you]]

【讨论】:

  • 非常感谢两位!这正是我想要的。但我想知道这两个程序中哪一个是最快的?
【解决方案2】:
public static void main(String[] args) {
   String[] sentences = { "hello", "how are you", "i am fine", "and you ?", "thank you" };

   List<List<String>> convertIntoList = convertIntoList(sentences, 2);
   System.out.println(convertIntoList);

   convertIntoList = convertIntoList(sentences, 3);
   System.out.println(convertIntoList);
}


private static List<List<String>> convertIntoList(String[] sentences, int nbElement) {
   List<List<String>> listOfListTarget = new ArrayList<List<String>>();
   int currentIndex = 0;

   while (currentIndex < sentences.length) {

    int nextIndex = currentIndex + nbElement;
    if (nextIndex > sentences.length) {
      nextIndex = sentences.length;
    }
    final String[] copyOfRange = Arrays.copyOfRange(sentences, currentIndex, nextIndex);
    List<String> subList = new ArrayList<String>();
    subList.addAll(Arrays.asList(copyOfRange));
    listOfListTarget.add(subList);
    currentIndex+=nbElement;
  }
  return listOfListTarget;
}

【讨论】:

  • 非常感谢两位!这正是我想要的。但我想知道这两个程序中哪一个是最快的?
【解决方案3】:

这是作业吗?

所以你有一个字符串数组,你想用它创建一个 List>,每个内部 List 最多包含 x 个元素。

要获取 x 个元素并将它们放入一个列表中,您可以执行一个简单的 for 循环。

String[] myStringArray = { ... };
List<String> myListOfString = new ArrayList<>();
for(int i=0; i<x; i++) {
    myListOfString.add(myStringArray[i]);
}

例如,如果你有这些值

String[] myStringArray = {"a", "b", "c", "d", "e"};
x = 2;

您将使用上述循环获得以下列表:

["a", "b"]

太棒了!但是我们需要得到myStringArray的所有内容!我们如何做到这一点?然后我们做第一步,我们遍历数组的所有内容。我们可以这样做。

int i=0; 
while(i < myStringArray.length) {
    System.out.println(myStringArray[i]);
    i++;
}

将输出:

a
b
c
d
e

这并不能解决问题……但至少我们知道如何迭代整个事情。下一步是获取其中的 x 个。听起来很简单吧?所以基本上我们需要从内容中创建一个 x 列表。也许我们可以使用我们创建几个示例的逻辑来解决问题。

// Create list of list of string here
int i = 0;
while(i < myStringArray.length) {
  // Create list of string here
  for(int j=0; j<x; j++) {
      // Add myStringArray[j] to list of string here
  }
  // Add the list of string to the list of list of string here
  i++;
}

简单吧?不,这给出了以下列表:

["a", "b"]
["a", "b"]
["a", "b"]
["a", "b"]
["a", "b"]

为什么?在第一个循环中,我们迭代到数组中有多少。在第二个循环中,我们将元素 0 和 1 添加到列表中。显然它是行不通的。第二个循环需要知道它不应该添加以前添加的元素,同时第一个循环需要知道第二个循环在做什么。所以你可能会想,也许我们可以使用int i 来指示第二个循环应该从哪里开始?

int i = 0;
while(i<myStringArray.length) {
    while(i<x) {
        // add myStringArray[i];
        i++;
    }
    i++;
}

不幸的是,使用与以前相同的值,这只会给出以下列表

["a", "b"]

因为i 正在遍历整个数组。因此,当它从 0 变为长度时,无论 i 的值在第二个数组上使用什么。再次循环时,i 变为 1,所以第二次循环的开始是 1。

我们需要一个单独的变量来进行计数,同时仍然记住我们当前在第二个循环中的位置。

int i = 0;
while(i<myStringArray.length) {
    int count = 0;
    while(count < x) {
        // Add myStringArray[count+i] to list of string
        count++;
    }
    // Add to list of list of string
    i += count + 1; // Need to be aware of how much we have processed
}

这将做我们想要的,但不幸的是,我们可能会在某些值上遇到麻烦。假设 x 为 10,myStringArray 的长度仅为 2。这将引发异常,因为当它到达 count+i = 3 时,该索引不再存在。第二个循环还需要知道还剩下多少。

最后我们将得到以下代码

int i = 0;
while(i<myStringArray.length) {
    int count = 0;
    while(count < x && count+i < myStringArray.length) {
        // Add myStringArray[count+i] to list of string
    }
    // Add to list of list of string
    i += count; // Need to be aware of how much we have processed
}

这会给你

["a", "b"]
["c", "d"]
["e"]

编辑:下次尝试放一些你尝试过的代码。

【讨论】:

    猜你喜欢
    • 2012-05-18
    • 2012-10-04
    • 2016-10-30
    • 2012-12-26
    • 1970-01-01
    • 2012-01-15
    • 2019-09-04
    • 1970-01-01
    • 2016-06-28
    相关资源
    最近更新 更多