简体   繁体   中英

How to change the value arrangement in array?

QUESTION: Given a String array of pokemon names:

 String[] pokemon={"Bulbsaur","Squirtle","Weedle","Pidgey","Rattata"};

a) Using a WHILE loop, write the code to display the pokemon names as shown

output:

Rattata

Pidgey

Weedle

Squirtle

Bulbsaur

(it is in the above order)

I then also try out the question:

public static void main(String[] args) {
    String[] pokemon= {"Bulbsaur","Squirtle","Weedle","Pidgey","Rattata"};
    int i=0;
    while(i<pokemon.length) {
        System.out.println(pokemon[i]);
        i++;
    }

}

All i want to know is that how to arrange the value like the output order. I know i can just change the position but i dont think the question says i can do so.

Thank you for helping!

Just iterate the array in reverse and print:

String[] pokemon= {"Bulbsaur","Squirtle","Weedle","Pidgey","Rattata"};
int i = pokemon.length - 1;
while (i >= 0) {
    System.out.println(pokemon[i--]);
}

If you actually needed to reverse the order in your Pokemon array, then I suggest doing an in-place swap:

String[] pokemon= {"Bulbsaur","Squirtle","Weedle","Pidgey","Rattata"};
int len = pokemon.length;
for (int i=0; i < pokemon.length/2; ++i) {
    String temp = pokemon[i];
    pokemon[i] = pokemon[len-1-i];
    pokemon[len-1-i] = temp;
}

The output is just printed the original array in reverse.

To reverse the array using while loop do the following.

public static void main(String[] args) {
    String[] pokemon= {"Bulbsaur","Squirtle","Weedle","Pidgey","Rattata"};
    int i=pokemon.length;
    while(i > 0) {
        System.out.println(pokemon[i-1]);
        i--;
    }
}

To print the array in reverse you have to start from the last element to the first element. The last element will be pokemon[pokemon.length - 1] and the first element will be pokemon[0]

In addition to iterating with an explicit index, you could create a ListIterator over the array and then use that to iterate your array backwards. Like,

String[] pokemon = { "Bulbsaur", "Squirtle", "Weedle", "Pidgey", "Rattata" };
ListIterator<String> iter = Arrays.asList(pokemon).listIterator(pokemon.length);
while (iter.hasPrevious()) {
    System.out.println(iter.previous());
}

Outputs (as requested)

Rattata
Pidgey
Weedle
Squirtle
Bulbsaur

Another option would be to create a List of names and reverse that and then iterate it normally. Like,

String[] pokemon = { "Bulbsaur", "Squirtle", "Weedle", "Pidgey", "Rattata" };
List<String> list = Arrays.asList(pokemon);
Collections.reverse(list);
Iterator<String> iter = list.iterator();
while (iter.hasNext()) {
    System.out.println(iter.next());
}
public static void main(String[] args) {

String[] pokemon={"Bulbsaur","Squirtle",
"Weedle","Pidgey","Rattata"};

   for(int i=pokemon.length;i > 0;i--) {

     System.out.println(pokemon[i-1]);
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM