java - 从方法返回数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26452633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:32:03  来源:igfitidea点击:

java - return array from method

javaarraysmethodsreturn

提问by user3683881

I created this snippet:

我创建了这个片段:

public static String[] get_data()
{
    conn = getInstance();
    String[] data_array = null;
    if(conn != null)
    {
        Statement query;
        try 
        {
            query = conn.createStatement();

            String sql = "SELECT data_x FROM table_x";
            ResultSet result = query.executeQuery(sql);

            result.next();
            int count = result.getInt("data_x");
            result.close();

            data_x_array = new String[count];

            for (int x = 1; x <= count; x++)
            {
                String data_x = result.getString(x);
                data_x_array[x] = data_x;
            }
        } 
        catch (SQLException e) 
        {
            e.printStackTrace();
        }
    }
    return data_x_array;
}

I just created a class, where data from the database is collected in an array.

我刚刚创建了一个类,其中将数据库中的数据收集在一个数组中。

Now i just want to return the array from this method.

现在我只想从这个方法返回数组。

But what i get is:

但我得到的是:

data_array cannot be resolved to a variable

Anybody could help me ?

任何人都可以帮助我吗?

Greetings!

你好!

UPDATE:

更新:

i changed snippet to:

我将片段更改为:

public static String[] get_data()
{
    conn = getInstance();
    String[] data_array = null;
    if(conn != null)
    {
        Statement query;
        try 
        {
            query = conn.createStatement();

            String sql = "SELECT data_x FROM table_x";
            ResultSet result = query.executeQuery(sql);

            result.next();
            int count = result.getInt("data_x");
            result.close();

            data_array = new String[count];

            for (int x = 1; x <= count; x++)
            {
                String data_x = result.getString(x);
                data_x_array[x] = data_x;
            }
        } 
        catch (SQLException e) 
        {
            e.printStackTrace();
        }
    }
    return data_x_array;
}

When i compile just:

当我编译时:

Invalid value for getInt() - 'value_in_table'

Anybody know this?

有人知道这个吗?

Greetings!

你好!

回答by ranifisch

Your declaration of the String[]is not in the same scope as the return statement.

您的声明String[]与 return 语句不在同一范围内。

You need to declare it in the beginning of the scope.

您需要在范围的开头声明它。

And you need to change the header of the function to:

您需要将函数的标题更改为:

public static String[] get_data()

回答by davek

You have defined your variable inside the whileloop i.e. it is not visible to the returnstatement. Plus, you have defined your method as static void, meaning no return value is expected. Use static String []instead.

您已经在while循环内定义了变量,即它对return语句不可见。另外,您已将方法定义为static void,这意味着不需要返回值。使用static String []来代替。

回答by SMA

Since its not accessible outside the block of while loop, compile is complaining about the same. Try this:

由于在 while 循环块之外无法访问它,因此 compile 抱怨相同。尝试这个:

public static String[] get_data()
{
conn = getInstance();
String[] data_array = null;
if(conn != null)
{
    Statement query;
    try 
    {
        query = conn.createStatement();

        String sql = "SELECT data_x FROM table_x";
        ResultSet result = query.executeQuery(sql);

        while (result.next()) 
        {
            String data_x = result.getString("data_x");
            data_array = new String[999];
            for (int x = 0; x <= 999; x++)
            {
                data_array[x] = data_x;
            }
        }
    } 
    catch (SQLException e) 
    {
        e.printStackTrace();
    }
}
return data_array;
}

回答by Tom

There are two things to fix here:

这里有两件事需要解决:

public static void get_data()

This method is declared to return nothing. Change it to:

此方法声明为不返回任何内容。将其更改为:

public static String[] get_data()

Your variable String[] data_arrayis declared in the while loop, therefore it is only known there. Your return statement is outside that loop, so it doesn't have access to it.

您的变量String[] data_array是在 while 循环中声明的,因此只有在那里才知道。您的 return 语句在该循环之外,因此它无法访问它。

Move the variable outside the loop:

将变量移出循环:

String sql = "SELECT data_x FROM table_x";
ResultSet result = query.executeQuery(sql);
String[] data_array = new String[999];

Mind that you need to move the declaration and the initialization outside the whileloop, or you will overwrite the previously stored data of that array by initializing it again. And also mind, that your forloop will overwrite the current data anyway ... you should think about storing the row data in another array so, or it will be lost.

请注意,您需要将声明和初始化移动到while循环之外,否则您将通过再次初始化来覆盖该数组先前存储的数据。还要注意,for无论如何,您的循环都会覆盖当前数据……您应该考虑将行数据存储在另一个数组中,否则它将丢失。

回答by SparkOn

One more thing everyone forgot to mention

还有一件事大家都忘了说

String[] data_array = new String[999];
for (int x = 0; x <= 999; x++){}

will throw an ArrayIndexOutOfBoundsException. Possible solution

将抛出一个 ArrayIndexOutOfBoundsException。可能的解决方案

String[] data_array = new String[999];
for (int x = 0; x < 999; x++){}