How to fill an ArrayList with values from another ArrayList randomly picked?

-2

I have class Vetor which will have a method to start a vector with random numbers:

public class Vetor{
    private int tamanho;
    private int[] vetor;

    public Vetor(int tamanho) {
       this.tamanho = tamanho;
       this.vetor = vetor;
    }

    public void iniciaVetor() {
        for (int i = 0; i < tamanho; i++) {
           this.vetor[i] = (int) Math.random();
        }
    }
}

Now I have the class Arraylist that will have a method to behave an array of vectors:

public class Arraylist{
    private int tamanho;
    private ArrayList<Vetor> arraylist;

    public Arraylist(int tamanho) {
        this.tamanho = tamanho;
        this.arraylist = new ArrayList<>();
    }

    public void iniciaArray(int tamanho_vetor) {
        Vetor v;
        for (int i = 0; i < tamanho_vetor; i++) {
            v = new Vetor(tamanho_vetor);
            v.iniciaVetor();
            arraylist.add(v);
        }
    }   
}

Finally, I have the class select that has a method that will randomly select an index (a vector from the list of vectors) and will fill that vector in the temp list. It will finish until the temp is the same size as arraylist .

public class Selecao{
    public ArrayList<Vetor> seleciona(ArrayList<Vetor> arraylist, tamanho_vetor) {
        ArrayList temp = new ArrayList<>();
        while (arraylist.size() != temp.size()) {
            int numero_aleatorio = (int) Math.random();
            //cromo adicionar o vetor da posição "numero_aleatorio" no arraylist temp
        }
    }
}
    
asked by anonymous 13.02.2017 / 15:34

1 answer

2

This code has several red flags, but I'll focus only on what you need for the question.

What you want is to shuffle the array . Java has it ready. Use the shuffle() . You do not even need to create a method for this, but if you want to create an abstraction you can do it:

import java.util.*;
import java.util.Collections;

class HelloWorld {
    public static void main (String[] args) {
         ArrayList<Integer> lista = new ArrayList<>();
         for (int i = 0; i < 50; i++) {
            lista.add(i);
         }
         seleciona(lista);
         for (int i = 0; i < 50; i++) {
            System.out.println(lista.get(i));
         }
    }
    public static void seleciona(ArrayList<Integer> arraylist) {
        Collections.shuffle(arraylist);
    }
}

See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .

I put Integer on the list, but could be anything else. I just did this to make it easier since the question did not have an easy code to test.

With the comment below it became clearer and I made this solution:

import java.util.*;
import java.lang.Math;

class HelloWorld {
    public static void main (String[] args) {
         ArrayList<Integer> lista = new ArrayList<>();
         for (int i = 0; i < 50; i++) {
            lista.add(i);
         }
         lista = seleciona(lista);
         for (int i = 0; i < 50; i++) {
            System.out.println(lista.get(i));
         }
    }
    public static ArrayList<Integer> seleciona(ArrayList<Integer> arraylist) {
        Random rnd = new Random();
        ArrayList<Integer> temp = new ArrayList<>(arraylist.size());
        for (int i = 0; i < arraylist.size(); i++) {
            temp.add(arraylist.get(rnd.nextInt(arraylist.size())));
        }
        return temp;
    }

}

See running on ideone . And No Coding Ground . Also put it on GitHub for future reference .

    
13.02.2017 / 17:38