0

In the below code I can get the length and element of array. if I have to check what is the index number at run time for every element, how can I check that? If I print the value of i from loop every time with the array element it will give the same value, will that be correct to consider the value of i as index value of array? Another confusion in during the debug in eclipse it shows id value of array is different than the loop value.

public class FirstArray {

    public static void main(String[] args) {
        int[] arr = {11,12,13,14,15,16,17,18,19,20};
        int onelength = arr.length;
        System.out.println("Size of Array is: " + onelength);
        for(int i = 0; i < arr.length; i++){
           System.out.println("element of aray is: "+ arr[i]);
        }       
     }
}
6
  • 1
    What do you mean by id value? Commented Jul 10, 2016 at 18:54
  • Your value of i will not be the index value of your element in the array. It will be i+1. Commented Jul 10, 2016 at 18:56
  • @VatsalSura Java has 0 based indexing. So it depends how you consider indexing. Commented Jul 10, 2016 at 18:58
  • @AkashdeepSaluja Yes you are right, I considered 1 based indexing. Commented Jul 10, 2016 at 18:59
  • what we will call the value which shows during debug process in variable window for every element, it show different id value from the loop bound index value for every element? i m just trying to clear my confusion from the index value defined in loop bound and the id value which generate during the debugging process for every element. Commented Jul 10, 2016 at 20:41

5 Answers 5

0

Yes, value of i will be the index value. I would suggest you to go through basics of Java arrays.

Sign up to request clarification or add additional context in comments.

Comments

0

The question itself is not clear. The loop bounds are definitely different from the index value of the array. If you want to print the loop bounds along with the value at the index, just print i in the loop.

1 Comment

loop bounds will print the value of i and if initiate the value of i from 0 to some other value it will start printing value from that particular index to the length of array, now confusion what value JVM consider during the debugging of array where the id value shows different(in debug window and variable window) from the loop value for every element.
0

For your question "what is the index number at run time for every element, how can i check that?" Refer to the solution bellow: Where is Java's Array indexOf?

For your question "If i print the value of i from loop every time with the array element it will give the same value, will that be correct to consider the value of i as index value of array?"

The array index starts from 0, so if your array length is 10 then index values will be 0 to 9. Thus, if you start your loop from i=0 then the index value will be same as i, but if you start your loop from i=1 then the index value will be i-1.

Comments

0

Will that be correct to consider the value of i as index value of array?

Of course it'll be correct, i is actually the index of the array.

Another confusion in during the debug in eclipse it shows id value of array is different than the loop value

Yes, it shows because it's really different, take a look:

for(int i = 0; i < arr.length; i++) {
    System.out.println("element of aray is "+ arr[i]); // It prints the element itself -> 11 12 13 14 15.. and so on
    System.out.println("iteration number "+ i); // It prints the index of iteration -> 0 1 2 3 4 5.. and so on
}     

3 Comments

here its printing the value which is defined in loop in second print line, i have confusion, is there any indexing managed by JVM during the debug process which shows different id value of element than the loop indexing?
Well, it's correct, the first print line prints the element (11, 12, 13.. and so on) because you're acessing the array in the i position.. and the second print line prints just the current index of iteration (0, 1, 2, 3.. and so on).
i'm not sure about the id value which generate during the debugging process for the array for every element. how it is managed by the JVM for every element?
0

You may want to clarify what exactly you are searching for.

An array stores a value at a given index (starting at index zero, and going up to index length-of-the-array-minus-one).

The traditional way of creating an array is the following:

// Create an empty array that is able to hold 3 values
int[] numbers = new int[3];
numbers[0] = 11;
numbers[1] = 15;
numbers[2] = 13;

If we now print the values in the index order, we receive 11, 15 and 13. Here's the code:

for (int i = 0; i < numbers.length; i++) {
    System.out.println(numbers[i]);
}

So, we see with numbers[i] = 14 we can assign the value 14 to the index i of the array. And with System.out.println(numbers[i]), we can print the value the array has stored at index i.
An array has a fixed length which needs to be specified at creation, it is not a flexible data structure (but pretty fast and small). Thus, if you are trying to access numbers[100] but we said numbers can only hold 3 values, then you will get an ArrayIndexOutOfBoundsException.

Your provided code is a short-hand for the traditional way:

int[] arr = {11,12,13};

which does the same as

int[] arr = new int[3];
arr[0] = 11;
arr[1] = 12;
arr[2] = 13;

If you want to search for the index, given the value (assuming the values are unique), you need to search the whole array until you find the index. Here's some code:

public int getIndex(final int[] array, final int value) {
    for (int i = 0; i < array.length; i++) {
        if (array[i] == value) {
            // Value found
            return i;
        }
    }
    // Value not found
    return -1;
}

Note that the search code is pretty slow, because you may need to search the whole length of the array (worst case). Other data structures may be more useful depending on your usage.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.