I am removing elements from an array in the below code. In this particular code I am removing the element at pos 2. How would I go about removing a random element in this array?
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]);
}
}
}
int size = countries.length - 1or justcountries.length - 1instead of a fixed size; otherwise you're doing it wrong.