-3

I have a long Array, and for every long value i m using this query to fetch a string Array from sqlite database.

long[] mylong= {1,3,5,6,7};
Dbadapter finaldb = new Dbadapter(this);
for(int i=0; i<mylong.length; i++) {
        finaldb.open();
        String[] string_array = finaldb.getArray(mylong[i]);
        finaldb.close();

now, i want to store these different string arrays with some name so that i can use them after. but i don't know how to achieve this. If any one can guide me in the right direction?

5
  • java.util.Map<Long, String[]>
    – Smutje
    Commented Oct 13, 2014 at 6:17
  • 1
    You can store them in a List<String[]> (to access by index) or Map<String,String[]> (to access by a String key)
    – Eran
    Commented Oct 13, 2014 at 6:17
  • create a class that contains a long variable and a String array. Use this object collection to store the result.
    – dasrohith
    Commented Oct 13, 2014 at 6:18
  • @dasrohith can you elaborate? Commented Oct 13, 2014 at 6:21
  • not getting the reason behind too many down votes. Commented Oct 13, 2014 at 6:29

3 Answers 3

0

You can have ArrayList<String[]> to store arrays and add those array to list and use get method to retrieve the array or you can use Map<key,value> to manage the key value based structure for retrieval of array.

FOR EXAMPLE

String str[]=new String[10];
str[0]="abc";
//and other values
List<String[]> list=new ArrayList<String[]>();
list.add(str);
//OR
Map<Long,String[]> map=new HashMap<Long,String[]>();
map.put(myLong[0],str);
7
  • Can you please give an example, that how can i use this to store arrays using for loop? Commented Oct 13, 2014 at 6:18
  • Are you sure about your first solution that it will store different string arrays? Commented Oct 13, 2014 at 7:07
  • It will store String arrays. Commented Oct 13, 2014 at 7:08
  • Ok, so what if we have to access the items of second or third String array? Commented Oct 13, 2014 at 7:12
  • list.get(2)[0] and list.get(3)[0] 0 stands for array index Commented Oct 13, 2014 at 7:14
0

Create a class that contains a long variable and a String array. Use this object collection to store the result.

See code snippet below

Class Result{
private long key;
private String[] value;


public Result(long key, String[] value){
this.key = key;
this value = value;
}

//getter setter goes here
}

modified code below

long[] mylong= {1,3,5,6,7};
List<Result> result = new ArrayList<Result>();
Dbadapter finaldb = new Dbadapter(this);
for(int i=0; i<mylong.length; i++) {
        finaldb.open();
        String[] string_array = finaldb.getArray(mylong[i]);
        result.add(new Result(mylong[i]),string_array ));
        finaldb.close();

You can use the List for further operations

0

I'm not sure if I really understand what you want to do, but it seems like you want to store the String[] db results in a Map using a String name.

Below I give an example how to do that. You can easily put this in a loop if you understand the concept. Hopefully this makes sense. I tried to match what you might actually be using.

String[] names = {"one", "two", "three", "four"};
String[] string_array = {"1.0", "2.1", "7.9"};
Map<String,String[]> storage = new HashMap<String,String[]>();

storage.put(names[0], string_array);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.