简体   繁体   English

在java中打印数组中的每个单词

[英]printing each word in an array in java

public class namePrinting 
{
    //each name in a series of names
    public String[] names = {"Hello", "My", "Name", "Is", "Jose"};

    //the method for putting names in 
    public static void printNames(String [] namesArray) 
    {
        //go through each String in the array and then print each
        for(int i = 0; i <= namesArray.length; i++)
        {
            System.out.println(namesArray[i]);
        }
    }
    //print the names in this specific array
    printNames(names[]);
}

I'm pretty new to Java and was wondering what I was doing wrong?我对 Java 很陌生,想知道我做错了什么? I just want to go through each string in the array and print each.我只想遍历数组中的每个字符串并打印每个字符串。 I think the incorrect portion is at the bottom... is there a specific variable I am forgetting to put inside printNames?我认为不正确的部分在底部......是否有我忘记放入printNames的特定变量?

Your code contains multiple errors:您的代码包含多个错误:

  1. You're using the names array in a static method, but the names themselves aren't static.您在静态方法中使用names数组,但名称本身不是静态的。 Either remove the static from the method, or add static to the field.从方法中删除静态,或向字段添加静态。
  2. printNames(names[]); is on class-level, which isn't possible.是在班级级别,这是不可能的。 Method calls should be in a method or constructor.方法调用应该在方法或构造函数中。
  3. names[] as parameter input isn't valid either. names[]作为参数输入也无效。 This should have been printName(names) instead.这应该是printName(names)代替。
  4. <= should be < . <=应该是< Because the length is 5 for your array, but the indices are [0-4].因为数组的长度为 5,但索引为 [0-4]。

Try something like this (without static):尝试这样的事情(没有静态):

public class NamePrinter
{
    //each name in a series of names
    public String[] names = {"Hello", "My", "Name", "Is", "Jose"};

    //the method for putting names in 
    public void printNames(String [] namesArray) 
    {
        //go through each String in the array and then print each
        for(int i = 0; i < namesArray.length; i++)
        {
            System.out.println(namesArray[i]);
        }
    }

    public static void main(String[] a){
        NamePrinter namePrinter = new NamePrinter();

        //print the names in this specific array
        namePrinter.printNames(namePrinter.names);
    }
}

Try it online. 在线试一下。

Or alternatively with static:或者使用静态:

public class NamePrinter
{
    //each name in a series of names
    public static String[] names = {"Hello", "My", "Name", "Is", "Jose"};

    //the method for putting names in 
    public static void printNames(String [] namesArray) 
    {
        //go through each String in the array and then print each
        for(int i = 0; i < namesArray.length; i++)
        {
            System.out.println(namesArray[i]);
        }
    }

    public static void main(String[] a){
        //print the names in this specific array
        printNames(names);
    }
}

Try it online. 在线试一下。

  1. Change:改变:

    for(int i = 0; i <= namesArray.length; i++)

    to:到:

    for(int i = 0; i < namesArray.length; i++) , for(int i = 0; i < namesArray.length; i++) ,

    because last iteration causes ArrayIndexOutOfBoundsException (length of names array is equal to 5, but last index is equal to 4 - indices of an array start from 0).因为最后一次迭代会导致ArrayIndexOutOfBoundsExceptionnames数组的长度等于 5,但最后一个索引等于 4 - 数组的索引从 0 开始)。

  2. Change printNames(names[]) to printNames(names) , because syntax that you have used is incorrect.printNames(names[])更改为printNames(names) ,因为您使用的语法不正确。

  3. If you want to use names array in the static method, this array should be declared as static too, on the other hand, you can delete static keyword from printNames() method.如果要在static方法中使用names数组,则该数组也应声明为static ,另一方面,您可以从printNames()方法中删除static关键字。

  4. You can't call printNames(names) in the body of class as you did it.你不能像你那样在类的主体中调用printNames(names) It should be put in some method or in the initialization block (I'm pretty sure that you should look to put it inside public static void main(String args[]) method.它应该放在某个方法或初始化块中(我很确定你应该把它放在public static void main(String args[])方法中。

To correct your code you have to :要更正您的代码,您必须:

  • Call your printNames method from a main methodmain方法调用您的printNames方法
  • Change your i <= namesArray.length to i < namesArray.length将您的i <= namesArray.length更改为i < namesArray.length

Your code gonna look like this :你的代码看起来像这样:

public class namePrinting 
{
    public static void printNames(String [] namesArray)
    {
        for(int i = 0; i < namesArray.length; i++)
        {
            System.out.println(namesArray[i]);
        }
    }

    public static void main(String[] args) {
        String[] names = {"Hello", "My", "Name", "Is", "Jose"};
        printNames(names);
    }
}

Try it online! 在线试试吧!

But to simplify your code you can use foreach instead of for loop :但是为了简化您的代码,您可以使用 foreach 而不是 for loop :

public static void printNames(String [] namesArray)
{
    for (String name: namesArray) {
        System.out.println(name);
    }
}

Try it online! 在线试试吧!

And if you use Java 8 or +, you can use Stream Api like this :如果您使用 Java 8 或 +,则可以像这样使用 Stream Api:

public static void printNames(String [] namesArray)
{
    Arrays.stream(namesArray).forEach(System.out::println);
}

Try it online! 在线试试吧!

Have fun ;)玩得开心 ;)

1 Change printNames(names[]) to printNames(names) 1 将printNames(names[]) to printNames(names)更改printNames(names[]) to printNames(names)

2 Use public static String[] names = {"Hello", "My", "Name", "Is", "Jose"}; 2 使用public static String[] names = {"Hello", "My", "Name", "Is", "Jose"}; instead of public String[] names = {"Hello", "My", "Name", "Is", "Jose"};而不是public String[] names = {"Hello", "My", "Name", "Is", "Jose"};

3 Use for(int i = 0; i <= namesArray.length -1 ; i++) instead of for(int i = 0; i <= namesArray.length; i++) 3 使用for(int i = 0; i <= namesArray.length -1 ; i++)而不是for(int i = 0; i <= namesArray.length; i++)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM