S
S
Sergey Sergey2021-09-01 00:29:43
Java
Sergey Sergey, 2021-09-01 00:29:43

Does the line in the code ref = index[y] equate to the math function y = f(x)?

public class TestArrays {
    public static void main(String[] args) {        
        String[] islands = new String[8];
        islands[0] = "Бермуды";
        islands[1] = "Фиджи";
        islands[2] = "Азорские острова";
        islands[3] = "Косумель";
        islands[4] = "Четыре";
        islands[5] = "Пять";
        islands[6] = "Шесть";
        islands[7] = "Семь";
        int[] index = new int[8];
        index[0] = 1;
        index[1] = 3;
        index[2] = 0;
        index[3] = 2;
        index[4] = 5;
        index[5] = 7;
        index[6] = 4;
        index[7] = 6;
        int ref;
        int y = 0;
        while (y < 8){
             ref = index[y];
            System.out.print("острова = ");
            System.out.println(islands[ref]);
             y = y + 1;
            } 
            }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BorLaze, 2021-09-01
@Sergei1111

Where does such a conclusion come from?
The loop runs 8 times through the index array, its contents select a value from the island array.
That is,
y = 0 -> index[0] = 1 -> islands[1] = "Fiji"
y = 1 -> index[1] = 3 -> islands[3] = "Cozumel"
and so on.
By the way, the code is just terrible. It's better to sort it out like this

public class TestArrays {

    public static void main(String[] args) {
        String[] islands = new String[]{
                "Бермуды",
                "Фиджи",
                "Азорские острова",
                "Косумель",
                "Четыре",
                "Пять",
                "Шесть",
                "Семь"
        };
        
        int[] index = new int[]{
                1, 3, 0, 2, 5, 7, 4, 6
        };

        for (int i = 0; i < index.length; i++) {
            int ref = index[i];
            System.out.println("острова = " + islands[ref]);
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question