dimanche 7 février 2016

randomly remove element from array java

I am removing elements from an array in the below code. So in this particular code I am removing the element at pos 2. Would anybody be able to help me in how I would go about removing a random element in this? public class QuestionOneA2 {

public static void main(String[] args) {
    int size = 5;
    int pos = 2;

    String[] countries = {"Brazil", "France", "Germany", "Canada", "Italy", "England"};

        for (int i = 0; i < size; i++) {
            if(i == pos) {
                countries[i] = countries[size];
            }
            System.out.println(countries[i]);
        }


    }

}




Aucun commentaire:

Enregistrer un commentaire