简体   繁体   中英

Get and set array values from instance in arraylist

I can't figure out how to get and set array values from a instance of a class that's in a arraylist. Someone who knows how?

Adding players code:

This code is for a school bingo project. In this code "SpelerLijst" is a bingo Players list. It is a random amount of players between 1 and 5.

import java.util.ArrayList;
import java.util.Random;

public class BingoPaneel {
    public BingoPaneel(){
        Random rand = new Random();
        ArrayList<BingoKaart> SpelerLijst = new ArrayList<BingoKaart>();
        //Aanmaken van spelers
        int spelers =  rand.nextInt((5-2 +1)+2);
        while (spelers == 0){
            spelers =  rand.nextInt((5-2 +1)+2);
        }
        System.out.println(spelers + " Spelers");
        for(int i = 0; i < spelers; i++){ 
        SpelerLijst.add(new BingoKaart()); 

        }

    }
}

Player code: This code gives the player a card thats named "Kaart"(The array) with 25 random ints.

import java.util.Arrays;
import java.util.Collections;
import java.util.Random;


public class BingoKaart {
    Random random = new Random();
    public BingoKaart(){
        int[][] kaart = new int[5][5];
        System.out.println("Spelerkaart: ");
        kaart= GetNumer(kaart);



    }
    private int[][] GetNumer(int[][] kaart) {       
        for(int i= 0; i < 5 ; i++){
            int[] getallen = new int[15];
            for(int k= 0; k < 15; k++){
                getallen[k] = k + (i*15);
            }
            shuffleArray(getallen);

            for(int j = 0; j < 5; j++){
                kaart[i][j] = getallen[j];
            }
        }

        //KAART PRINTEN
        for(int i = 0; i < 5; i++){
            for(int j = 0; j < 5; j++){
                System.out.print(kaart[i][j]+ " ");
            }
            System.out.println();
        }
        System.out.println();


        return kaart;
    }
     static void shuffleArray(int[] ar)
      {
        Random rnd = new Random();
        for (int i = ar.length - 1; i > 0; i--)
        {
          int index = rnd.nextInt(i + 1);
          int a = ar[index];
          ar[index] = ar[i];
          ar[i] = a;
        }
      }


}

Master code: This code runs trough random ints and checks if one of the players has the number in its array.

import java.util.Random;

public class BingoMaster {

    public BingoMaster(){
        //Vars
        int[] getrokkenGetallen = new int[75];
        boolean bingo = false;

        //Vullen en randomizen trekbare getallen
        VulGetrokkenGetallen(getrokkenGetallen);

        System.out.println("Het spel Start!");
        while(!bingo){
        TrekGetal(getrokkenGetallen);
        }

    }


    private int[] VulGetrokkenGetallen(int[] getrokkenGetallen) {       

        for(int i = 0; i < 75; i++){
            getrokkenGetallen[i] = i;
        }
        shuffleArray(getrokkenGetallen);
        return getrokkenGetallen;


    }

    private void TrekGetal(int[] getrokkenGetallen) {
        for(int i = 0; i < 75; i++){
            int getal = getrokkenGetallen[i];

        }
    }

    static void shuffleArray(int[] ar)
     {
        Random rnd = new Random();
        for (int i = ar.length - 1; i > 0; i--)
        {
          int index = rnd.nextInt(i + 1);
          int a = ar[index];
          ar[index] = ar[i];
          ar[i] = a;
        }
      }



}

I just can't figure out how to check if the number is in the array of a player. The system to save the checked number is a thing that i'll build as the next step.

I'm just going to point out:

ArrayList<BingoKaart> SpelerLijst = new ArrayList<BingoKaart>();

Variables by Java convention shouldn't be capitalized—only class names, which helps a lot in organization of your code.

Anyway, to answer your question, I'll build on to the first answer.

So, first may I ask, are the classes that you want to access it in the same package? If they are, all you need to do is keep the ArrayList instance as it is, or add protected (because hey, in my opinion, protected looks cooler than nothing, and does the same thing plus allows sub class access). Otherwise, if they're in different packages, label it as public, so all classes can access it (in fact, you can just label it public anyway, because it doesn't make a difference, and it's an advantage in the long run).

Next step: instantiating the class

BingoPaneel <var name> = new BingoPaneel();

After instantiating, you can then do

bp.SpelerLijst.add(<Something from BingoKaart>);

to access the ArrayList.

Or, if you don't feel like instantiating (bad idea), you can make it static, and do

BingoPaneel.SpelerLijst.add(<Something from BingoKaart>);

You need to be able to access SpelerLijst outside of the class. Move your declaration of SpelerLijst outside of your constructor in BingoPaneel and then you can call it by saying the following-

BingoPaneel bp = new BingoPaneel();

//do something here with the arrayList by calling bp.SpelerLijst

You should define SpelerLijst as an instance variable, outside the constructor. Add the getters and seters so you can access it when instantiating BingoPanel.

public class BingoPanel(){

List<BingoKaart> spelerLijst;

public BingoPanel(){
spelerLijst=new ArrayList<>();
//load all the data.
}

//getters and setters..

Also in BingoKaart, the int[][] kaart, should be declared outside, like the previous one, the way you are doing, when it finish the constructor, the values are lost.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM