how to create a vector vector?

Asked

Viewed 767 times

1

Can anyone tell me how to create a vector in java? i.e.: How to make a vector store other vectors with int values

this loop populates a vector with values from 1 to 4 q are directions N=North, S=South, L=East, O=West.

for (int i =0; i < aux; i ++) {

        v[i] = (int) (Math.random() *4);

        if(v[i] == 0){
             N++;
        System.out.println("N");}
        if(v[i] == 1){
            S++;
        System.out.println("S");}
        if(v[i] == 2){
            L++;
        System.out.println("L");}
        if(v[i] == 3){
            O++;
        System.out.println("O");} 
    }

my goal is to store the results in other 4 vectors

  • Welcome to the OS, Joyce! The concept you are looking for would be that of matrices, perhaps? v[i][j]

  • 1

    Array of array of how many sizes? 2? is just add a couple more [] in front q Voce adds one more dimension.

  • the array is of varying size because I use an Rand to assign any value( between 0 and 10) to the aux variable. Then in the end there are 4 arrays with varying sizes and random numbers inside. Maybe matrix is the solution wanted q to look like this array1 = N O L S O L L N O L array2 = N N N N L O S L O L .... etc where q each letter represents a number q will be counted

2 answers

1

You can also use HashMap. See code below:

import java.util.HashMap;
import java.util.Map;

public class SingleObject {

    private static SingleObject instance;

    private static Map<String,Integer> pontos = new HashMap<String,Integer>();

    private SingleObject() {
        pontos.put( "N", 0);
        pontos.put( "S", 0);
        pontos.put( "L", 0);
        pontos.put( "O", 0);
    }

    public static SingleObject getInstance(){
        if(instance == null)
            instance = new SingleObject();
        return instance;
    }

    public void increment(String opcao) {
        Integer opc = pontos.get(opcao);
        pontos.replace(opcao, opc + 1);
    }

    public void exibir() {
        System.out.println(pontos);
    }

    public static void main(String[] args) {
        SingleObject o = SingleObject.getInstance();
        o.increment("L");
        o.increment("N");
        o.increment("N");
        o.increment("N");
        o.increment("O");
        o.exibir();

        o.increment("L");
        o.increment("O");
        o.increment("N");
        o.increment("S");
        o.increment("S");
        o.increment("O");
        o.exibir();
    }
}
  • I really want to return an arraylist filled with random principal vectors, no matter if it is repeated or not. Ex: Let’s assume 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 does not matter if you drop vector number 6 again, if it falls, I will have to fill in temp anyway in another index. then msm q would be a vector q would guard another vector

0

You can create a list of vectors using the List interface (read more about the interface and its methods by clicking here), implementing the Arraylist class. Following your example, it would look something like this:

List<int[]> listaDeVetores = new ArrayList<int[]>();
for (int i =0; i < aux; i ++) {
    v[i] = (int) (Math.random() *4);

    if(v[i] == 0){
        N++;
        System.out.println("N");}
    if(v[i] == 1){
        S++;
        System.out.println("S");}
    if(v[i] == 2){
        L++;
        System.out.println("L");}
    if(v[i] == 3){
        O++;
        System.out.println("O");}
    //adiciona o vetor da vez na lista.
    listaDeVetores.add(v);
}

But analyzing the problem that you propose, there are always 4 "directions" that exist, so would create a class with attributes N,S,L and O, and would make a list only of this class and not a list of vectors, thus making it easier to maintain and handle the attributes.

  • I told the colleague above I want to return an arraylist filled with random principal vectors, no matter if it is repeated or not. Ex: Let’s assume 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 does not matter if you drop vector number 6 again, if it falls, I will have to fill in temp anyway in another index. then msm q would be a vector q would guard another vector

Browser other questions tagged

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