English 中文(简体)
两维数组中的第一个元素无效吗?
原标题:First Element in TwoDimensional Array goes to null?

我正使用此代码将细节插入二维数组。 但是在从数组检索数据时, 第一个元素值更改为空 。

    Cursor consultancy = db.getConsultancy(this);
        if(consultancy!=null)
        {
            consultancy.moveToFirst(); 
            consultancy.moveToNext();  
            consultancynames = new String[(int) db.getConsultancyCount()-1];
            for(int i=0;i<db.getConsultancyCount()-1;i++)
            { 
                consultancynames[i] = consultancy.getString(2);  
                int consultantid = Integer.parseInt(consultancy.getString(consultancy.getColumnIndex(TimeAndExpensesLocalDB.CT_CONSULTANCYID))); 
                Cursor project_namecur = db.getProjectCode(this, consultantid);
                if(project_namecur!=null)
                {
                    project_namecur.moveToFirst();  
                    projectname = new String[(int) db.getConsultancyCount()][project_namecur.getCount()];
                    for(int j=0;j<project_namecur.getCount();j++)
                    {  
                        projectname[i][j] = project_namecur.getString(3);    
                        project_namecur.moveToNext();  
                    } 
                }
                consultancy.moveToNext();
            }  

        }  

        //... Print array  
        for (int i =0; i < consultancynames.length; i++) {
        for (int j = 0; j < projectname.length; j++) {
        System.out.print(" " + projectname[i][j]);
        }
        System.out.println("");
        }

产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出

       05-25 12:58:22.700: I/System.out(2373):  null null null
       05-25 12:58:22.700: I/System.out(2373):  Other-1 Other-2 Other-3

我不知道发生了什么事情。

谢谢你的帮助,伙计们...

最佳回答

您正在循环的每次迭代上创建一个新阵列 :

projectname = new String[(int) db.getConsultancyCount()][project_namecur.getCount()];

所以,在第一个迭代中,您正在创建一个数组并在数组的 first “row”中填充。在第二个迭代中,您正在创建一个新的数组(默认为无元素),并在 second 列中填充。

我怀疑您需要分配“ 外” 数组, 一旦 < ether> 在 < / em> 循环之前, 就需要分配“ 内” 数组, 然后根据该顾问有多少项目名称来分配“ 内” 数组 :

 // Note: more idiomatic names would be consultancyNames and
 // projectNames. It s also unclear why you re subtracting one from the count...
 consultancynames = new String[(int) db.getConsultancyCount() - 1];
 projectnames = new String[consultancynames.length][];
 for (int i = 0;i< consultancenames.length; i++) {
     ...
     projectnames[i] = new String[project_namecur.getCount())];
     ...
 }

然后您也需要更改您的显示代码, 例如, 更改到

for (int i =0; i < projectname.length; i++) {
    for (int j = 0; j < projectname[i].length; j++) {
        System.out.print(" " + projectname[i][j]);
    }
    System.out.println("");
}
问题回答

请注意,您不能做下列事情:

projectname = new String[(int) db.getConsultancyCount()][project_namecur.getCount()];
for(int j=0;j<project_namecur.getCount();j++)
{  
    projectname[i][j] = project_namecur.getString(3);    
    project_namecur.moveToNext();  
}

此处说明原因:

在第一行projectname 之后,将是一个数组。

由于数组是对象引用,所以您有对象引用的数组。

由于对象引用的默认值为 null , 您将拥有一系列 null 元素。

这意味着你不能做

projectname[i][j] = project_namecur.getString(3);  

因为它相当于

String[] row = projectname[i];
// row == null !
row[j] = project_namecur.getString(3);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签