1

I created the following code:

int storagevar= arrayList.get(0);

for (int j=0; z<(storagevar-1); j++){
  someString= normalArray[j];
}

This code looks through an ArrayList and gets the first element in there, which is a number representing an index in a second (normal) array containing Strings. It then uses this number to get the information from the second array, and stores it in another String.

This code is completely fine; it's the next bit thats causing a problem.

The above code assumes there is only 1 element inside the ArrayList. However, if there is more than one element, then I need to do the operation for each element in the ArrayList, so if there were 3 elements inside it, I would end up with the 3 indexes, and I would use them to extract the information from the normal array, and will end up with 3 Strings.

I wrote the code below for the above scenario. I know it doesn't store each element answer in its own String, and I don't know how to do it; I need help with that too. But anyways, this code doesn't seem to work either:

public void testMethod(){

MyClass test_one = arrayList.get(8);
String[] tmpStringArray = test_one.correct;
ArrayList<Integer> nnaCorrectAnswers = test_one.correctAnswers;

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

    tmp2= nnaCorrectAnswers.get(i);

    for (int z=0; z<(tmp2 -1); z++){
    someString=tmpStringArray[z];
    }
}
}
CosmicGiant
  • 6,275
  • 5
  • 43
  • 58
  • 2
    Create an`ArrayList` and store the results there. – Hovercraft Full Of Eels Dec 25 '15 at 16:10
  • @HovercraftFullOfEels Yeah I did that after posting the question, but the loop doesn't work, it prints very weird when debugging – Blueaddiction Dec 25 '15 at 16:11
  • Why do you use a loop to initialize always the same `someString` variable? The loop could be replaced with `someString = normalArray[storagevar - 2]` – JB Nizet Dec 25 '15 at 16:12
  • @JBNizet I need to store each string in its own variable, wouldn't the way you are suggesting it not work? Because it'll override what was in someString before it. – Blueaddiction Dec 25 '15 at 16:16
  • Also the code doesn't print out the elements I want it to, is it not identical to the top one but just allowing multiple variables? – Blueaddiction Dec 25 '15 at 16:17
  • 1
    That's exactly what you are doing: overwriting the value stored in someString at each iteration. Hence my comment: only the last iteration is useful. All the preceding ones are useless. – JB Nizet Dec 25 '15 at 16:17
  • @JBNizet I realized this problem after posting the code, ive changed it to a String arraylist now and ill be able to iterate through that, but the code still doesn't do what I want it to any ideas why? – Blueaddiction Dec 25 '15 at 16:20

2 Answers2

1

If I understand correctly, you have an List<Integer> indices containing indices. For example, [2, 4, 6].

And you have an array strings containing Strings. For example: ["a", "b", "c", "d", "e", "f", "g", "h"].

And you want to create a list containing all the elements of the array whose indices are stored in the list. So, for example: ["c", "e", "g"] (because "c" is at index 2, "e" is at index 4, and "g" is at index 6).

Is that right? If so, all you need is:

List<String> result = new ArrayList<>();
for (int index : indices) {
    result.add(strings[index]);
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Your explanation for what I need is correct, I substituted your values for mine but when running I get indexoutofbounds – Blueaddiction Dec 25 '15 at 16:33
  • 1
    That means that one of the indices is negative, or greater or equal then the length of the array. Either that should never happen, and you have a bug elsewhere, or it can happen, and you need to check that the index is valid before accessing the array element. – JB Nizet Dec 25 '15 at 16:35
  • I have an arrayList of size 3 [1,2,5] and a string array of 5 elements which throws indexoutofbounds – Blueaddiction Dec 25 '15 at 16:39
  • 1
    If your array has 5 elements, its indices are 0, 1, 2, 3 and 4. So 5 is not a valid index. Hence the exception. – JB Nizet Dec 25 '15 at 16:40
  • Nizel how can I initialise the index to start at 1? I dont use array loops much – Blueaddiction Dec 25 '15 at 16:43
  • You can't. Indices always start at 0. But you can use `result.add(strings[index - 1])` if the indices in the list start at 1 instead of starting at 0. I would just store actual indices in the list instead, though. Get used to indices starting at 0. – JB Nizet Dec 25 '15 at 16:45
  • result.add(strings[index - 1]) - worked thanks! I just thought you could make it start at 1 like a for loop, I'll look into this for each loop more thanks again – Blueaddiction Dec 25 '15 at 16:48
  • @Blueaddiction - If you can change the values stored/loaded in the `ArrayList`, then preferably, already store them as if the indexes started from 0 instead of 1. In other words, using the 3-element exemple from comments above, store them as `[0,1,4]`, instead of `[1,2,5]` plus a `index-1` later. – CosmicGiant Dec 25 '15 at 17:08
  • I've run into another error I have now stored all the strings I need but this code doesnt work, when I click a button I get nullpointer exception http://pastebin.com/cQXgay7s – Blueaddiction Dec 25 '15 at 17:25
  • Read http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – JB Nizet Dec 25 '15 at 17:26
0

Instead of always getting index 0, do a for-each loop:

for(int number : arrayList){
  someString = normalArray[number-1];
  //...
}
CosmicGiant
  • 6,275
  • 5
  • 43
  • 58