-3

我还是java新手,所以我想寻求帮助,我不太清楚在这种情况下如何使用方括号,不。我什至不知道如何使用它,我尝试了一些东西,结果如下,我现在的问题是我想添加:

schemeName += (rset.getString("scheme_name"));
schemeId += (Integer.parseInt(rset.getString("id")));

但我得到了一个错误,因为我认为我没有正确使用它。感谢您的帮助并向我解释如何使用它以及有什么问题。

所有代码:

String[] schemeName; // TODO, []
Integer[] schemeId; // TODO, []
        String HTML = "";

            Connection conn = null;
            try
            {
                conn = L2DatabaseFactory.getInstance().getConnection();

                PreparedStatement statement = conn.prepareStatement("SELECT * FROM buffer_scheme_list WHERE player_id=?");

                statement.setInt(1, player.getObjectId());
                ResultSet rset = statement.executeQuery();

                while (rset.next())
                {
                    try
                    {
                        schemeName += (rset.getString("scheme_name"));
                        schemeId += (Integer.parseInt(rset.getString("id")));
                    }
                    catch(Exception e)
                    {
                        // Blank
                    }
                }
            }

如果我改为:

 String schemeNamel
 Integer schemeId;

我在进一步的代码中遇到了另一个问题(所以我不能这样做);进一步的代码:试过这个,但后来我在进一步的代码中遇到了另一个问题:

if (schemeName.length > 0)
                    {
                        String MESSAGE = "";
                        String Temp = "";

                    int i=0, j=0;
                    Temp="<tr><td> </td> <td> </td></tr>";
                    String[] TRS = Temp.split(" ");
                    while (i <= schemeName.length - 1)
                    {
                        if (j>2)
                        {
                            j = 0; // vienas is dvejiu arba if j>2 (&& arba ||), j=0;
                            MESSAGE += TRS[j]+"<button value=\""
                            +schemeName[i]+
                            "\" action=\"bypass -h Quest "+QUEST_LOADING_INFO+" cast "
                            +schemeId[i]+" x x\" width=130 height=25 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\">"
                            +TRS[j+1];
                        }

                        i+=1;
                        j+=2;
                    }`
4

3 回答 3

2

你已经声明了一个数组

String[] schemeName; // TODO, []
Integer[] schemeId; // TODO, []  

您正在尝试为数组赋值

schemeName += (rset.getString("scheme_name"));
schemeId += (Integer.parseInt(rset.getString("id")));

你不能这样做。

数组是一个容器对象,它包含固定数量的单一类型的值。数组的长度是在创建数组时确定的。创建后,它的长度是固定的。

此外,您还没有初始化两个数组

于 2013-09-26T13:06:20.350 回答
1

schemeName并且schemeId是数组。他们不支持+操作。

此外,您没有初始化它们,所以它们仍然是null.

我建议使用某种集合,例如 List 或 Set。他们支持一种add方法。

这是一个使用示例ArrayList

ArrayList<String> schemeName = new ArrayList<String>();
ArrayList<Integer> schemeId = new ArrayList<Integer>();

...

schemeName.add((rset.getString("scheme_name"));
schemeId.add(Integer.parseInt(rset.getString("id")));

如果您以后更喜欢使用数组,可以将列表转换为数组,如下所示:

String[] schemeNameArray= schemeName.toArray(new String[schemeName.size()]);
Integer[] schemeIdArray= schemeId.toArray(new Integer[schemeId.size()]);

如果schemeNameschemeId类似于 value 和 key,则使用 Map。他们支持put

于 2013-09-26T13:05:11.510 回答
0

您不需要为字符串和整数包含 []。您可以简单地将它们声明为

String schemeName = "";

Int schemeId = 0;

于 2013-09-26T13:07:25.100 回答