import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import org.omg.CORBA.PUBLIC_MEMBER;
public class random_number {
public static boolean check(ArrayList<Integer> flag, int n) { // n == array size
int c = 0;
for(int i=0; i<n; i++) {
if(flag.get(i) == 1) {
c++;
}
}
if(c == n){
return true;
}
return false;
}
public static boolean idCheck(ArrayList<Integer> flag, int index) {
if(flag.get(index) == 0) {
return true;
}
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Enter the size of array");
int ArraySize = input.nextInt();
//input.close();
Random random = new Random();
ArrayList<Integer> arrayList = new ArrayList<Integer>();
for(int i=0; i<ArraySize; i++) {
int r = input.nextInt();
arrayList.add(r);
}
ArrayList<Integer> flag = new ArrayList<Integer>();
for(int i=0; i<ArraySize; i++) {
flag.add(0);
}
int i = 0;
while(i<10) {
int index = random.nextInt(ArraySize); // array size
if(idCheck(flag, index)) {
flag.set(index, 1);
System.out.println(i + " " + arrayList.get(index));
i++;
}
if(check(flag, ArraySize)) {
System.out.println("No elements in the array");
break;
}
else {
continue;
}
}
input.close();
}
}
import java.util.Random;
import java.util.Scanner;
import org.omg.CORBA.PUBLIC_MEMBER;
public class random_number {
public static boolean check(ArrayList<Integer> flag, int n) { // n == array size
int c = 0;
for(int i=0; i<n; i++) {
if(flag.get(i) == 1) {
c++;
}
}
if(c == n){
return true;
}
return false;
}
public static boolean idCheck(ArrayList<Integer> flag, int index) {
if(flag.get(index) == 0) {
return true;
}
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Enter the size of array");
int ArraySize = input.nextInt();
//input.close();
Random random = new Random();
ArrayList<Integer> arrayList = new ArrayList<Integer>();
for(int i=0; i<ArraySize; i++) {
int r = input.nextInt();
arrayList.add(r);
}
ArrayList<Integer> flag = new ArrayList<Integer>();
for(int i=0; i<ArraySize; i++) {
flag.add(0);
}
int i = 0;
while(i<10) {
int index = random.nextInt(ArraySize); // array size
if(idCheck(flag, index)) {
flag.set(index, 1);
System.out.println(i + " " + arrayList.get(index));
i++;
}
if(check(flag, ArraySize)) {
System.out.println("No elements in the array");
break;
}
else {
continue;
}
}
input.close();
}
}
No comments:
Post a Comment