68 447
Assignments Done
98,6%
Successfully Done
In December 2018

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.
Expert's answer
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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions