简体   繁体   English

在Java中将数组从一个方法调用到另一个方法时遇到问题

[英]Having trouble calling an array from one method to another in Java

The way I have my code set up is I declare my array in one method then I want to print it out in a table like fashion in another. 我设置代码的方式是我在一个方法中声明我的数组,然后我想在另一个表格中将它打印出来。 But I want to do this while only using the main() function. 但是我想在只使用main()函数的情况下这样做。

I have taken most of the irrelevant code out so here is my code: 我已经把大部分不相关的代码拿出去了,所以这里是我的代码:

public static void main(String[] array) {
    test2(array);
}

public static void test() {
    String[] array = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16"};
}

public static void test2( String[] array ) {
    int count = 0;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) { 
            System.out.print(array[count] + "\t"); 
            count++;
        }   
        System.out.println();
        System.out.println();
        System.out.println();
    }
}

When I try and run it, it comes up with the java.lang.ArrayOutOfBound on the line "System.out.print(array[count] + "\\t");" 当我尝试运行它时,它会在“System.out.print(array [count] +”\\ t“)行中找到java.lang.ArrayOutOfBound;”

Does anyone know why this is and/or how to fix it? 有谁知道这是为什么和/或如何解决它?

You have several errors: 你有几个错误:

  1. You create array as a local variable in test() . 您在test()中将array创建为局部变量。
  2. You use the arguments of the application as parameters. 您使用应用程序的参数作为参数。
  3. You don't even call test() . 你甚至不打电话给test()

The consequences are that you call your application probably with no parameters and end up having the test2() method trying to access the first element of an empty array, causing your exception. 结果是你可能没有参数调用你的应用程序,并最终让test2()方法试图访问空数组的第一个元素,导致你的异常。

This is what you should do, but keep reading after the code, I'm not done: 这是你应该做的,但继续阅读代码后,我没有完成:

public static void main(String[] args) { // This array is defined, but don't use it.
  test2(test());
}

public static String[] test() {
  return new String[]{"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16"};
}

public static void test2( String[] array ) {
  int count = 0;
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) { 
      System.out.print(array[count] + "\t"); 
      count++;
    }   
    System.out.println();
    System.out.println();
    System.out.println();
  }
}

This code has still issues. 此代码仍有问题。 You indeed assume that you have 16 elements in your array. 您确实假设您的数组中有16个元素。 Yet you're not sure. 但你不确定。 Oh yes, you're sure because you've added them, but you shouldn't assume that it'll always be the case. 哦,是的,你确定,因为你已经添加了它们,但你不应该认为它总是如此。

Therefore it's nice to check for the actual number of the elements anyways. 因此,无论如何都要检查元素的实际数量。

public static void test2( String[] array ) {
  int count = 0;
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      if (count < array.length) {
        System.out.print(array[count] + "\t"); 
        count++;
      }
    }   
    System.out.println();
    System.out.println();
    System.out.println();
  }
}

Well, it doesn't work because when your "main" calls "test2(array)" it passes nothing, since main doesn't have an "array" defined in it. 好吧,它不起作用,因为当你的“main”调用“test2(array)”时它什么都没有传递,因为main没有定义一个“数组”。

The array that you want exists only inside the "test" method. 您想要的数组仅存在于“test”方法中。

So, one simple solution would be to change your code to: 因此,一个简单的解决方案是将代码更改为:

public static void main(String[] array) {
    test2(test());
}

public static String[] test() {
    String[] array = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16"};
    return array;
}

public static void test2( String[] array ) {
    int count = 0;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) { 
            System.out.print(array[count] + "\t"); 
            count++;
        }   
        System.out.println();
        System.out.println();
        System.out.println();
    }
}

...so that the method test() returns the array when it's called in main. ...这样方法test()在main中调用时返回数组。

But, still, the code doesn't make lots of sense, especially in Object Oriented programming. 但是,代码仍然没有多大意义,特别是在面向对象编程中。

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

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