How to fill an Arraylist with values of another Arraylist caught randomly?

Asked

Viewed 1,127 times

-2

I have the class Vetor which will have a method to start an array 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 got the class Arraylist which will have a method to hold a array vector:

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 selects which has a method that will randomly select an index (an array from the list of vectors) and will populate that vector in the list temp. It will end by the temp be 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
        }
    }
}
  • Create a ArrayList based on another ArrayList is simple: List<AlgumaCoisa> a = ...; List<AlgumaCoisa> b = new ArrayList<>(a); - however, this is what the title of your question asks. What the content of your question asks for, it seemed to me very confusing. I recommend you describe verbatim what you are trying to do instead of already going to code.

1 answer

2


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

What you want is to shuffle the array. Java has it ready. Use the method shuffle(). You don’t even have 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);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I put a Integer in the list, but it could be anything else. I only did so to facilitate 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;
    }

}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Thanks for the comment. But I don’t want you to just shuffle the arraylist. I really want to return an arraylist filled with random principal vectors, no matter if it is repeated or not. Ex: Vamor suppose that the vector number 6 is drawn. It contains 1, 5, 3, 8, 6. I want these values to be filled in at the first position of the temp arraylist. Then draw another vector and so on. Note that it doesn’t matter if you drop vector number 6 again, if it falls, I’ll have to fill in temp anyway in another index.

  • @Rrr I did as you say now.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.