Java 将字符串数组放入ArrayList的ArrayList中

Java 将字符串数组放入ArrayList的ArrayList中,java,arrays,arraylist,multidimensional-array,Java,Arrays,Arraylist,Multidimensional Array,我想将字符串数组转换为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"] //Out

我想将字符串数组转换为
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"]
publicstaticvoidmain(字符串[]args){
String[]句={“你好”,“你好吗”,“我很好”,“你呢?”,“谢谢”};
List convertIntoList=convertIntoList(句子,2);
System.out.println(转换列表);
convertIntoList=convertIntoList(句子,3);
System.out.println(转换列表);
}
私有静态列表convertIntoList(字符串[]句,int-nbElement){
List listOfListTarget=new ArrayList();
int currentIndex=0;
while(当前索引<句子长度){
int-nextIndex=currentIndex+nElement;
if(nextIndex>句子长度){
nextIndex=句子长度;
}
最后一个字符串[]copyOfRange=Arrays.copyOfRange(句子、currentIndex、nextIndex);
列表子列表=新的ArrayList();
addAll(Arrays.asList(copyOfRange));
添加(子列表);
currentIndex+=nbElement;
}
返回listOfListTarget;
}
这是家庭作业吗

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

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

String[] myStringArray = { ... };
List<String> myListOfString = new ArrayList<>();
for(int i=0; i<x; i++) {
    myListOfString.add(myStringArray[i]);
}
使用上述循环,您将获得以下列表:

["a", "b"]
太好了!但是我们需要获取mystringaray的所有内容!我们怎么做?然后让我们执行第一步,遍历数组的所有内容。我们可以这样做

int i=0; 
while(i < myStringArray.length) {
    System.out.println(myStringArray[i]);
    i++;
}
这并不能解决问题。。。但至少我们知道如何重复整个过程。下一步是得到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++;
}
为什么??在第一个循环中,我们迭代到数组中的数量。在第二个循环中,我们将元素0和1添加到列表中。显然,这是行不通的。第二个循环需要知道它不应该添加先前添加的元素,同时第一个循环需要知道第二个循环正在做什么。您可能会想,也许我们可以使用
inti
来指示第二个循环应该从哪里开始

int i = 0;
while(i<myStringArray.length) {
    while(i<x) {
        // add myStringArray[i];
        i++;
    }
    i++;
}
因为
i
正在遍历整个数组。所以当它从0变为长度时,无论第二个数组上使用的是什么值。当它再次循环时,
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
}
这将给

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

编辑:下次尝试输入您尝试过的代码。

元素数=2和3之间的区别是什么?@bibuche您想要代码吗?元素数必须是动态的。句子总数是5个。当元素数=2时,我们得到2,2,1。当元素数=3时,我们得到3,2作为内部Arraylist中的字符串数。非常感谢这两个!这正是我想要的。但是我想知道这两个项目中哪一个是最快的?非常感谢你们两个!这正是我想要的。但我想知道这两个项目中哪一个最快?
a
b
c
d
e
// 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"]
int i = 0;
while(i<myStringArray.length) {
    while(i<x) {
        // add myStringArray[i];
        i++;
    }
    i++;
}
["a", "b"]
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
}
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"]