Using arrays of arrays as pointers to expandable lists of classes in Java

Reading the first answer in the Passing a String by Reference in Java? I (as on old pointer freaked C nerd) have a question regarding using arrays as pointers in java.

I have a variably number of EditText boxes in a setup routine for an number of strings to be stored. This includes a add one EditText box button. So on screen the button is followed by an number of EditText boxes. I have a similar for URLs to be stored.

Instead of having the same like 20 lines of code repeated over and over again for different such setup data items it is quite obviously a case for a method (function in C) and the issue is how do I keep the information about what EditText boxes are created when the user is pushing the Save button. In C you just send a pointer of an array of editboxes and realloc the editboxes array if new editboxes are created. In Java I can't realloc a the editboxes array to expand it but can create and clone it. But then it is not the same the editboxes array. But then I have the old editboxes array in the calling method and not the new. But obviously with Strings it is possible to make a String array array of one unit and send to the method and get it updated. This is obviously possible to be used with editboxes arrays as well, making an editboxes array array of one editboxes array that could be updated by the called method and expanded.

String[][] stringList = new String[1][];
stringList[0] = new String[2];
stringList[0][0] = new String("Sonny");
stringList[0][1] = new String("Ronny");
ExpandArray(context, stringList);
public static void ExpandArray(Context context, String[][] stringPtr) {
    stringPtr[0][1]="Zeke";
    String[] strings = new String[3];
    System.arraycopy(stringPtr[0], 0, strings, 0, stringPtr[0].length);
    stringPtr[0] = strings;
    stringPtr[0][2]="Sue";
}

and

EditText[][] EditTextList = new EditText[1][];
EditTextList[0] = new EditText[2];
EditTextList[0][0] = new EditText(context);
EditTextList[0][1] = new EditText(context);
ExpandArray(context, EditTextList);
public static void ExpandArray(Context context, EditText[][] EditTextPtr) {
    EditText[] EditTexts = new EditText[3];
    System.arraycopy(EditTextPtr[0], 0, EditTexts, 0, EditTextPtr[0].length);
    EditTextPtr[0] = EditTexts;
    EditTextPtr[0][2]== new EditText(context);
}

Question 1 Reading the first answer in the Passing a String by Reference in Java? all comments are in strong favour of the two first StringBuilder solutions, but with no explanation why. That is Strings here we are talking about class arrays, might be different. *I feel I just made a C-solution in Java syntax and there might be some better Java culture solutions? You know C nerds are crazy about pointers to pointers etc and this smells such. *

Are there any better solutions for class arrays than arrays (solution 3)?

I am learning Java culture and my actual method of a button and n number of EditText boxes (works fine), but feel like garage culture:

public static LinearLayout TextListbox(Context context, EditText[][] EditTextboxes, String Title, String DataStringsDimension) {
    final Context contextTextListbox = context;
    final EditText[][] EditboxTextListbox=EditTextboxes;
    final String ItemText = Title;
    final LinearLayout layoutEditTextListbox = new LinearLayout(contextTextListbox);
    layoutEditTextListbox.setOrientation(LinearLayout.VERTICAL);
    layoutEditTextListbox.setX(layoutEditTextListbox.getX() + 15);
    layoutEditTextListbox.setBackgroundColor(Color.rgb(0xFF, 0xE8, 0xAA));

    if (DataStringsDimension != null)
        EditboxTextListbox[0] = new EditText[getJniListDataSize(DataStringsDimension)];
    else
        EditboxTextListbox[0] = new EditText[0];
    final Button button = new Button(contextTextListbox);
    button.setText("+ " + ItemText);
    button.setTag(EditboxTextListbox[0].length);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Enlarge to Edit box array
            EditText[] EditboxArrayTemp = new EditText[EditboxTextListbox[0].length + 1];
            System.arraycopy(EditboxTextListbox[0], 0, EditboxArrayTemp, 0, EditboxTextListbox[0].length);
            EditboxTextListbox[0] = EditboxArrayTemp;
            // Register a new edit box
            EditboxTextListbox[0][EditboxTextListbox[0].length - 1] = new EditText(contextTextListbox);
            EditboxTextListbox[0][EditboxTextListbox[0].length - 1].setHint(ItemText);
            EditboxTextListbox[0][EditboxTextListbox[0].length - 1].setInputType(InputType.TYPE_CLASS_TEXT);
            layoutEditTextListbox.addView(EditboxTextListbox[0][EditboxTextListbox[0].length - 1]);
        }
    });
    layoutEditTextListbox.addView(button);

    if (EditboxTextListbox[0].length > 0) {
        String[] DataStrings = getJniTextUnceListData(DataStringsDimension);
        for (int iSlotTitle = 0; iSlotTitle < EditboxTextListbox[0].length; iSlotTitle++) {
            EditboxTextListbox[0][iSlotTitle] = new EditText(contextTextListbox);
            EditboxTextListbox[0][iSlotTitle].setText(DataStrings[iSlotTitle]);
            EditboxTextListbox[0][iSlotTitle].setTag(DataStrings[iSlotTitle]);
            EditboxTextListbox[0][iSlotTitle].setHint(ItemText);
            EditboxTextListbox[0][iSlotTitle].setInputType(InputType.TYPE_CLASS_TEXT);
            layoutEditTextListbox.addView(EditboxTextListbox[0][iSlotTitle]);
        }
    }
    return layoutEditTextListbox;
}

Question 2 It certainly be improved as Java culture? Any smart ideas?

I have it as a method in the SetupDlg class of mine, any reason to make such a thing its own class?

There might be some better ideas of how to make a setup edit for n number of text strings to be edited?


You can just use ArrayList instead of Array. Array has a fixed size, but ArrayList can grow easily. Equivalent to this:

String[][] stringList = new String[1][];
stringList[0] = new String[2];
stringList[0][0] = new String("Sonny");
stringList[0][1] = new String("Ronny");
ExpandArray(context, stringList);
public static void ExpandArray(Context context, String[][] stringPtr) {
    stringPtr[0][1]="Zeke";
    String[] strings = new String[3];
    System.arraycopy(stringPtr[0], 0, strings, 0, stringPtr[0].length);
    stringPtr[0] = strings;
    stringPtr[0][2]="Sue";
}

will be:

List<List<String>> stringList = new ArrayList<List<String>>();
stringList.add(new ArrayList<String>());
stringList.get(0).add(new String("Sonny"));
stringList.get(0).add(new String("Ronny"));
ExpandArray(context, stringList);
//Instead of expand array
stringList.get(0).add(new String("Sue"));

For editText just use:

List<List<EditText>> editText = new ArrayList<List<EditText>>()

The size of ArrayList increases as required.

And about passing by reference, in java everything is passed by value. Only class objects are passed by reference. So only if you pass a class object and modify it, the change will be reflected outside the function.

For strings you can use string builders or String objects but if its only one string following works as well:

void main(){
    string txt = "foo";
    txt = addString(txt);
    System.out.println(txt);
}
String addString(String txt)
{
    txt += " bar";
}

The output will be : foo bar

Hope this helps!!

链接地址: http://www.djcxy.com/p/20898.html

上一篇: urllib for python 3

下一篇: 使用数组数组作为指向Java中可扩展的类列表的指针