I have an int array with random numbers. and I have a string array already filled as well.
I’m trying to display (System.out.print) the String array in an order corresponding to the order within the int array.
like, if the int array[]={3,1,2}, and if the inital String array[]={"a","b","c"}, then I’d like the string array to print as
stringArray[intArray]={c, a, b}
in this example, "a" at index 0 would correspond to number 1, etc.
I wondered if I could do something like (I tried, but I got an “out of bound” exception, so I guess it’s still possible) :
for (int i=0; i<intArray.length; i++) {
if(intArray[i] != 0) {
System.out.println( stringArray[ intArray[i] ] );
}
}
I also tried to set the length of the loop as length-1, without success.
Please note that I’m trying to make it work in a basic way, without objects or methods. I’ve read something about a TreeMap, but that’s already way too advanced.
Note that array indexes start with zero, try to decrement the value retrieved from the int array.