开发者

objects passed between 2 functions

I'm hoping to get a little feedback. I'm a beginner when it comes to anything android or java programming so not sure which field this falls under. I have 2 functions(is that the right term? like in VB) one returns a string array or the other an int array based on an Cursor I pass to it. What I've couldn't figure out for the longest was why was my int array function not returning any data when it acknowledged data in my Cursor. It turned out that the Cursor I would pass to my first function (the string array) would move the Cursor position as the function iterated thru the cursor data. After the string array function I would pass the cursor to my int array function. This one would not return any data in my int array, i was racking my head for a while until I realized it was simply seeing the end of the cursor and assuming it had done it's job. Once I added a cursor.moveToPosition(-1) it worked great. Does java/android not work as VB does? In VB.net I'm able to pass an object to a function that than has it's own copy of the object to work on without affecting the original object I passed. Sorry for the lengthy email, just didnt know how else to word it.

Maybe I wrote this wrong so I've added my code below so you guys can tell me if I'm doing something wrong.

CompanyList = mDbHelper.CompanyNames(searchParam, SearchedColumn);
String CompanyNames[] = mDbHelper.CursorToString(CompanyList,开发者_Python百科 "CompanyName"); 
final int CompanyID[] = mDbHelper.CursorToInt(CompanyList, "CompanyID");

public String[] CursorToString(Cursor cursor, String columnName){
    int cursorCount = cursor.getCount();
    if (cursorCount > 0 ){
        String[] str = new String[cursorCount];
        int i = 0;

        while (cursor.moveToNext()){
            str[i] = cursor.getString(cursor.getColumnIndex(columnName));
            i++;
        } 
        return str;
    }
    return new String[]{};
}

public int[] CursorToInt(Cursor cursor, String columnName) {
    int cursorCount = cursor.getCount();
    if (cursorCount > 0) {
        int[] number = new int[cursorCount];
        int i = 0;
        cursor.moveToPosition(-1);
        while (cursor.moveToNext()){
            number[i] = cursor.getInt(cursor.getColumnIndex(columnName));
            i++;
        }
        return number;
    }
    return null;
}


No, Java does not work that way. In general, when you move from one language to another, it is really never safe to assume that they work the same way. In VB you have the notion of ByVal and ByRef. In the first, a copy is made, in the second, the memory address is passed and any changes made on the variable will be reflected anywhere else it is referenced. Java works similarly to using ByRef for all variables that are not primitive types (such as int, double, etc).

My answer here will give you a bit more information and check out this article for a slightly more technical description of the way parameters are passed in Java.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜