Chintan Khetiya
Chintan Khetiya

Reputation: 16142

How to get same index value from other array?

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.

Upvotes: 1

Views: 2635

Answers (4)

Chintan Khetiya
Chintan Khetiya

Reputation: 16142

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)));

}

Upvotes: 0

Andremoniy
Andremoniy

Reputation: 34900

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

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

Upvotes: 1

BobTheBuilder
BobTheBuilder

Reputation: 19284

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));
}

Upvotes: 1

Lisa Anne
Lisa Anne

Reputation: 4595

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...))

Upvotes: 2

Related Questions