Serban Cosmin
Serban Cosmin

Reputation: 25

Indexing in java arrays

I had to write a method in Java, where having in input an array a of numbers and a number x returns an array of elements which follows the last occurrence of x in a.

For example with input {0,1,2,3,4,5,6,7,8,9} and x=6 the method must return {7,8,9} meanwhile with {4,1,4,2} and x=4 the method must return {2} and if the x is not in a then it must return empty array {} (or array with 0 length)

so I got this answer:

int idx = -1;
for (int i = 0; i < s.length; i++) { 
    if (s[i] == x)
        idx = i;
}

/* After you found this index, create a new array starting 
 * from this element. It can be done with a second (not nested) for loop, or you can
 * use Arrays.copyOfRange()
 */

//make sure idx != -1
int[] t = new int[s.length - idx - 1];
for (int i = idx + 1; i < s.length; i++)
    t[i - idx - 1] = s[i];

which was very helpful but I could not understand why this works:(EDITED; Ok now I understand why this works but even if in my opinion the combined for loop ideea was more less complicated to read)

t[i - idx - 1] = s[i];

and this doesn't:

int[] t = new int[a.length - indx - 1];
for (int j = indx + 1; j < a.length; j++) {
    for (int i = 0; i < t.length; i++) {
        t[i]=a[j];
    }
}
return t;

EDITED : To clarify this is all the code

int[] dopoX(int[] a, int x) {
    int n = a.length;
    int[] c = new int[0];
    int indx = 0;
    int nrx = 0;
    for (int j = 0; j < a.length; j++) {
        if (a[j] == x)
            nrx++;
        if (a[j] == x)
            indx=j;
    }
    if (nrx == 0)
        return c;   
    int[] t = new int[n - indx - 1];
    for (int j = indx + 1; j < n; j++) {
        for (int i = 0; i < t.length; i++) {
            t[i] = a[j]; /* it returns just 1 number of a[] like t{2,2,2,2,2,2,2,2} which is 
                            not correct */          
        }
    }
    return t;
}

Upvotes: 1

Views: 10655

Answers (4)

Daniel Persson
Daniel Persson

Reputation: 602

The first line of code finds the last index of x inside of the array.

The second line uses the built-in function of Arrays to copy a range from an array to a new copy.

And we copy from the values after the last x until lenght of array a.

The first line could be rewritten to search from the end and backwards in the array with a break. This will give a performance boost but makes the code less easy to read.

for(int i=0; i<a.length;i++) if(a[i]==x) idx=i;

int[] b = Arrays.copyRangeTo(a, idx+1, a.length);

Upvotes: 1

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476669

Well you want to copy all the remaining values, and create an array of index t. Thus you need to start with i=0. You can however perform a shift-operation: increase i somewhere, and when you use it, shift it back, so:

for (int i = idx+1; i < s.length; i++)
    t[i-idx-1] = s[i];

is equvalent to:

for (int i = 0; i < t.length; i++)
    t[i] = s[i+idx+1];

(which would have been more readable as well)


About your second question:

here you use a nested loop: the second for loop will be repeated each iteration of the first one.

The result is thus that in the second for-loop, j is always fixed, with input {1,2,...,9} and 6 in the first iteration, you would fill your array with 7s, next 8s and finally 9s.

You can however use a combined for loop:

int []t=new int[n-indx-1];
//       /-- grouped initializers      /-- grouped increments
//       |                             |
for(int i=0, j= indx+1; i < t.length; i++, j++){
    t[i]=a[j];
}
return t;

Upvotes: 1

vls1
vls1

Reputation: 39

Try this:

int j=0;
int i=idx+1;
while (j<t.length) {
    t[j] = s[i];
    j++;
    i++;
}

Upvotes: 0

shauryachats
shauryachats

Reputation: 10385

Let's suppose you take the case where a[] = {1,2,3,4,5,6,7,8,9} and x = 6. Running this:

int idx = -1;
for (int i = 0; i < s.length; i++) { 
if (s[i] == x) idx = i;
}

You got idx = 5 as s[5] == x.

Now we want to copy the array after the last instance of x into a new array t[].

Obviously, you have to start from the index idx + 1 as idx contained the last occurance of x.

Hence, this code:

int[] t = new int[s.length - idx - 1];
for (int i = idx+1; i < s.length; i++)
    t[i-idx-1] = s[i];

What do you do here?

You construct a new array t[] having length s.length - idx - 1, in our case s.length = 9 and idx = 5 hence, we have the s.length - idx - 1 as 3 and we can check that is the number of elements after x = 6.

Now, we start the iterator i from idx + 1 (Reason explained above) to s.length

We have t[i - idx - 1] because when i = idx + 1, i - idx - 1 = 0. Hence as i increases, your i - idx - 1 also increases.

Hope it was convincing. Please comment if you still have any doubts.

Upvotes: 1

Related Questions