Java 带nextLine条件的While循环

Java 带nextLine条件的While循环,java,arrays,next,Java,Arrays,Next,我正在为一个处理数据库数据的类开发一个方法。我正在尝试使用while循环和.nextLine为我的数组值[]执行System.out.println,我希望有人能提供一些建议。我知道还有其他方法可以做到这一点,但我希望不要使用任何额外的变量。如果这不可能,我完全理解,但我想这里一定有人知道办法。谢谢你的帮助,这是我的方法 public void query(String table,String... column) { System.out.println("name of t

我正在为一个处理数据库数据的类开发一个方法。我正在尝试使用
while循环
.nextLine
为我的数组
值[]
执行
System.out.println
,我希望有人能提供一些建议。我知道还有其他方法可以做到这一点,但我希望不要使用任何额外的变量。如果这不可能,我完全理解,但我想这里一定有人知道办法。谢谢你的帮助,这是我的方法

    public void query(String table,String... column)
{
    System.out.println("name of table is: " + table);
    System.out.println("column values are: ");

    while(column[].nextLine())
        {
            System.out.println(column.nextLine());
        }

}//end method query()
nextLine()
扫描器的方法,而不是
字符串的方法。如果你真的有一个
字符串数组
s,你可以用一个(增强的)
for
循环:

public void query(String table, String... column) {
    System.out.println("name of table is: " + table);
    System.out.println("column values are: ");

    for (Strinc c : column) {
        System.out.println(c);
    }
}
for (int i = 0; i < column.length; i++) {
    System.out.println(column[i]);
}
nextLine()
扫描器的方法,而不是
字符串的方法。如果你真的有一个
字符串数组
s,你可以用一个(增强的)
for
循环:

public void query(String table, String... column) {
    System.out.println("name of table is: " + table);
    System.out.println("column values are: ");

    for (Strinc c : column) {
        System.out.println(c);
    }
}
for (int i = 0; i < column.length; i++) {
    System.out.println(column[i]);
}
您可以使用增强的for(也称为for each)循环:

或正常的
循环:

public void query(String table, String... column) {
    System.out.println("name of table is: " + table);
    System.out.println("column values are: ");

    for (Strinc c : column) {
        System.out.println(c);
    }
}
for (int i = 0; i < column.length; i++) {
    System.out.println(column[i]);
}
注意:

请记住,
column
是一个数组:
String[]column

您可以使用增强的for(也称为each)循环:

或正常的
循环:

public void query(String table, String... column) {
    System.out.println("name of table is: " + table);
    System.out.println("column values are: ");

    for (Strinc c : column) {
        System.out.println(c);
    }
}
for (int i = 0; i < column.length; i++) {
    System.out.println(column[i]);
}
注意:

请记住,
column
是一个数组:
String[]column
nextLine()
不是数组的方法。不仅如此,你还用错了。您应该这样做(如果这些方法存在):
while(column.hasNextLine())

假设要使用while循环打印字符串数组:

int i = 0;
while(i < column.length)
{
    System.out.println(column[i]);
    i++; // increment the index
}
甚至是经典的for循环:

for (int i = 0; i < column.length; i++) {
    System.out.println(column[i]);
}
for(int i=0;i
nextLine()
不是数组的方法。不仅如此,你还用错了。您应该这样做(如果这些方法存在):
while(column.hasNextLine())

假设要使用while循环打印字符串数组:

int i = 0;
while(i < column.length)
{
    System.out.println(column[i]);
    i++; // increment the index
}
甚至是经典的for循环:

for (int i = 0; i < column.length; i++) {
    System.out.println(column[i]);
}
for(int i=0;i
仅仅对于note
不是字符串引用,它类似于
String[]
@JigarJoshi,事实上,我将其视为一个数组。仅仅对于note
不是字符串引用,它类似于
String[]
@JigarJoshi,事实上,我将其视为一个数组。for-each似乎是最优雅的解决方案感谢您的帮助您的答案是最全面的。谢谢是的,这是最优雅的。如果不需要计算索引/位置,请使用它。for-each似乎是最优雅的解决方案感谢您的帮助您的答案是最全面的。谢谢是的,这是最优雅的。如果您不需要计算索引/位置,请使用它。