可以说我有一个像这样的数组:
['x','cat', 'dog', 'x', 'dolphin', 'cougar', 'whale']
我不知道数组的长度或何时会出现'x'.当我到达'x'时,我想将以下元素推送到一个新数组中,直到我到达下一个元素includes?('x')
.
期望的输出是:
[['cat', 'dog']['dolphin','cougar', 'whale']]
我怎样才能做到这一点?
我正在尝试代码一个基本的应用程序,它将获取用户想要输入的数字量,然后向用户询问数字,将数字保存在字符串数组中,然后以特定格式打印出来.
这是我到目前为止唯一的问题是我需要删除输出结束时的最后一个逗号,我确定我会发生这种错误......任何帮助都表示赞赏.
import java.util.Scanner;
public class info {
public void blue(){
Scanner sc = new Scanner(System.in);
Scanner dc = new Scanner(System.in);
System.out.println("how many numbers do you have?");
int a = dc.nextInt();
System.out.println("enter your numbers");
String index[]=new String [a];
String i;
for(int k = 0;k<index.length;k++){
i = sc.nextLine();
index[k]=i;
}
System.out.print("query (");
for(int l = 0;l<index.length;l++){
System.out.printf("'%s',",index[l]);
}
System.out.print(")");
}
}
Run Code Online (Sandbox Code Playgroud)