0

I have

String[] x1 = new String[] { "05:30", "07:20", "13:30", "21:30"};
String[] x2 = new String[] { "08:30", "01:20"}; 
String[] x3 = new String[] { "12:30", "00:20", "13:54"}; 

From a database I receive x1 or x2 or x3 name.

String get_x = null;
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
    do {
        get_x = cursor.getString(0); // get_x will have value x1 or x2 or x3
    } while (cursor.moveToNext());
}

And then I want to get size of returned x[1 or 2 or 3] eg. int size = x1.length, where x1 is value of get_x string.

I tried

int size = get_x.length; 

but not working. 'size' will be equal with length of get_x, not length of x1.length

Aly Ty
  • 1
  • 2

2 Answers2

1

It sounds like you need to use a more appropriate data structure for your program. You have several options:

  1. Store the value directly in the database rather than storing a value which acts as an "index" for the value.
  2. Change your database to return an int value (e.g. 1, 2, or 3) rather than a String (e.g. "x1", "x2", "x3") and use an an array or List instead of three separate variables. The int value from the database will act as an index to your array or List.
  3. Leave your database as it is and use Map rather than three distinct variables. Now you can use the value from the database as an index into your Map.

The following questions on Stack Overflow are related to your issue:

Is it possible to create variables at runtime in Java?

How to create a variable with a user-defined name in Java?

Community
  • 1
  • 1
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

Here is an answer using reflection :). Pass get_x to the method. "YourClassName" - is the name of your Activity (or the class hosting your code), replace it.

            public void getSize(String arrayParamName){
                Field[] fields = YourClassName.class.getDeclaredFields();

                for (Field field : fields) {

                    //gives the names of the fields
                    if(field.getName().equals(arrayParamName)) {
                        System.out.println(field.getName());
                        try {

                            String[] returnedArrayFromDB = (String[])field.get(CameraActivity.this);
                            System.out.println("length of " +field.getName() + ":  "+ returnedArrayFromDB.length);
                            System.out.println("first value of array " +returnedArrayFromDB[0]);


                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
  • many thanks. One more question, is it possible to read a value from x1 using for? e.g. for (int i=0; i – Aly Ty Jan 25 '16 at 00:03
  • See edited version. After you cast the field to String[] you can use it as usual. – Roman Rozenshtein Jan 26 '16 at 20:05
  • With that said and I am glad that it did resolved your problem, I encourage you to use a different approach in the future as Code-Apprentice wrote. In short, it can be done but reflection is not what you would like to use in these situations. – Roman Rozenshtein Jan 26 '16 at 20:08