0

I'm still learning about sorting and Arrays. Came up with this code which does sorting letters or numbers on ascending order but I really don't get the last part where System.out.println(SampleArray[i]);. Why is it SapleArray[i]? can someone enlighten me please.

public class TestClass {    
    public static void main(String[] args) {
        String SampleArray[] = {"B","D","A","R","Z"};       
        Arrays.sort(SampleArray);
        for (int i=0; i < SampleArray.length; i++) {
                System.out.println(SampleArray[i]);
        }
    }
}
1
  • Note that your array is sorted in place which means that the original order is lost. Commented Apr 20, 2014 at 9:54

4 Answers 4

1

SampleArray refers to the whole array - here containing five strings.

SampleArray[0] refers to the first item in the array (here the string "B").

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

lets i take the values 0, 1, 2, 3, 4 one after another, so you print out first SampleArray[0]and then SampleArray[1] and so on until all the items have been printed.

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

2 Comments

Thank you for this explanation and everyone else's answers. I think I get it now, the variable i was used because it could go through all the length of the array for the sorting to be possible but it just so happen than I don't know much about arrays, I never knew declaring SampleArray[i] was possible.
Also, you should name your array sampleArray. The convention is that SampleArray would indicate a class, not a variable.
1

You are trying to print an array, which is an ordered collection of items.

SampleArray[i] inside the loop lets you access one element at a time, in order.

More specifically, i takes on the values 0 through 4 in this case.

SampleArray[0] would give you the first element of SampleArray, because Java uses zero-based indexing for arrays.

2 Comments

I know it is printing the Array but I don't quite understand how, I mean the variable i is not even part of the array, it basically counts the arrays.
@rsarellano. First, there is only one array. The variable i is being used to index into this array, and allows us to, in some sense "pull out" elements from the array and print them.
1

Going through your code:

first you create a String array with those letters as element, which are saved like this: element 0 of array = B, element 1= D, etc. (in Arrays the counting always begin by 0 and not by 1).

Then it sort it in ascending order.

The for loop is there to print the sorted array, it iterate through the array beginning by element 0, until it is at the last element of the Array and it print these element.

Comments

1

The for loop does something over and over again.

The first part of the for loop, int i = 0 sets up a variable.

The second part i < SampleArray.length is the 'terminating condition' - it's the condition that has to remain true before each iteration of the loop.

The last part i++ is what happens after each iteration - i gets incremented.

So we are printing each element within SampleArray. i goes between 0 and the one less than the number of elements in the array. (e.g. if the array contained 4 elements, i would be 0 to 3, which is what we want).

And in the body of the loop, the [i] bit selects that element from SampleArray, and that is the value that gets printed on each line.

Another way of looking at it: SampleArray supports the [] operator, which when applied, will return an element from the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.