Answer to Question #62115 in Java | JSP | JSF for Fensa

Question #62115
Write a java method that repeatedly selects and removes a random entry from an array until the array holds no more entries.
1
Expert's answer
2016-09-20T11:15:03-0400
import java.util.Random;

/**
* Created by sakura on 9/19/16.
*/
public class Main {
public static void main(String[] args) {
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
removeElements(array);
}

static void removeElements(int[] array) {
Random r = new Random();
while (array.length > 0) {
int index = r.nextInt(array.length);
System.out.println("INDEX = " + index + ", ELEMENT = " + array[index]);
int[] array1 = new int[array.length - 1];
for (int i = 0; i < index; i++)
array1[i] = array[i];
for (int i = index; i < array.length - 1; i++)
array1[i] = array[i + 1];
array = array1;
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS