understand Array Object, and what I want to do is call my array in every class I have

Roman :

I'm trying to understand Array Object, and what I want to do is call my array in every class I have.

this is my code:

projectProva.java

public class ProjecteProva {
        Scanner sc = new Scanner(System.in);
        private final int maxContador = 4;
        private final DadeArr LlistaUsuari[] = new DadeArr[maxContador];      

            int ContadorActual;
        }

DadeArr.java

public class DadeArr {
            private String nomUsuari;
            private String cognomUsuari;
            public DadeArr(String nU, String nC){
            nomUsuari = nU;
            cognomUsuari = nC;
        }

Right now I'm working in projectProva.java , I have some method that saves into array a data input with scanner.

Here is an example of one of my method:

        public int inserir(int aContadorActual){
            ContadorActual = 1;
              for (int i=1;i<=ContadorActual;++i){

                    System.out.println("Introdueix el nom del usuari: ");
                    String nU = sc.nextLine();
                    //sd.setNomUsuari(Name);
                    System.out.println("Introdueix el teu cognom : ");
                    String nI = sc.nextLine();
                    LlistaUsuari[ContadorActual] = new DadeArr(nU,nI);
                    System.out.println("El teu usuari s'ha creat             satisfactoriament");


               }
                    ContadorActual++;
                    return ContadorActual;
        }

This method asks user his name and surname and saves it in array LlistaUsuari.

Then, I want to use this array(with the data in) in another .java file from the same package, but i don't know how to properly call the array.

I just started to learn this type of array, and i want to understand it.

After solving this , im looking forward to take all array info and send it to a data base or text file.

If I can't proceed with it i will switch to Array 2D.

Plus, I'm wondering if this type of Array ( array object ) is very usefull or not.

Thanks. I also made this question at https://www.reddit.com/r/javahelp/comments/dsyu4b/array_object/?

Shankha057 :

You have multiple gotchas in your code.
1. you should always iterate an array from index 0 (unless you have special needs or you are programming in a language like Python where arrays start from 0)
2. You should set the condition in the loop to be less than the exact length of the array. So in your case it will be i < LlistaUsuari.length. You are getting a null because you are only filling up index #1 of the array.

All you need is a public getter method for the array that you want to access.
Something like:

public DadeArr[] getLlistaUsuari() {
    return this.LlistaUsuari;
}

And that should do it.
In other classes, create an instance of ProjecteProva (lets's call it pPI) and to get the array there, simply do pPI.getLlistaUsuari() and you'll have it,

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=357072&siteId=1