1

I have two arrays and i want to access data of same index value from other array.

Two Array list :

ArrayList<Integer> Position = new ArrayList<Integer>();
ArrayList<String> List_Data = new ArrayList<String>();

Now my Position array contains Integer value like index of data i.e 0,3,5 out of 10 Records. i want to get only those string whose index should be i.e 0,3,5 out of 10 .

Example :

String Array >> [A,B,C,D,E,F,G,H,J,K]; 

Index >> Now i am selecting 2 ,5 index data.

Final Output as string >> C,F

So at the end i get actual string from array.

I get this and some other link also but not get exact idea how to do this.

Please anyone help me.

3
  • Why not try to fix your problems with using more suitable Collections such as Maps or simply ArrayList<Integer, String> Commented Mar 4, 2013 at 13:21
  • there are some other things with same operation. so its simply that get data according index Commented Mar 4, 2013 at 13:22
  • Your given example and linked image don't do the same thing. Which are you trying to do? Commented Mar 4, 2013 at 13:24

4 Answers 4

2

Try this, If I understand what you want correctly (otherwise let me know)

String sr=Lista_Data.get(Position.get(INDEX YOU NEED; EG 1, 5, 1000...))
Sign up to request clarification or add additional context in comments.

3 Comments

get(Position.get(INDEX YOU NEED; EG 1, 5, 1000...)) here , what is Position ?
Position is your ArrayList that you called Position.
this operation i am doing inside button's click event. so get only list.get(int)
1

You can get object from ArrayList using get function. Then you can use it as an index to another ArrayList.

String res = "";
for (Integer pos : Position) {
    res += List_Data.get(Position.get(pos));
}

3 Comments

i am not able to get Position in get(int index) ;
You first need to populate those arrays
What do you mean? you dont have Position ArrayList?
1

The only thing you need is method indexOf(...) of List.

public String getStringByIndex(Integer index) {
    return List_Data.get(Position.indexOf(index));
}

1 Comment

i tried by this way spinner_Data.indexOf(posSel.get(i)); but force close
0

I am not not saying that above code is wrong but its not working according my needs. or i can't handle because of my other code limitation.

Finally, I get as i want like :

for (int i = 0; i < Poisition.size(); i++)
{

 System.out.println("Selected Data --->"+ List_Data.get(Poisition.get(i)));

}

Comments

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.