user3383541
user3383541

Reputation: 21

How print the array in another class?

Basically, i have a class where i have my arrays in, which is like this

    public final class DepotDatabase {
    private Driver[] arrayDrivers;

     public DepotDatabase() {
        arrayDrivers = new Driver[4];

    arrayDrivers[0] = new Driver(1234, 1234, 0); // sample driver
    arrayDrivers[1] = new Driver(4444, 4444, 0); // sample driver
    arrayDrivers[2] = new Driver(1337, 1337, 1);   // sample manager
    arrayDrivers[3] = new Driver(1234, 1234, 0); // sample driver

}

and i want to print this array in another class, i did set up the array in another class

public Driver(int username, int password, int managerCheck) {
    this.username = username;
    this.password = password;
    this.managerCheck = managerCheck;
}

but now i want to be able to print out all the drivers, but in another class which will be called ViewDrivers or something similar

Upvotes: 1

Views: 8616

Answers (3)

Salah
Salah

Reputation: 8657

You can create a method inside DepotDatabase to print the array, then create an object from and call print method.

public final class DepotDatabase {
private Driver[] arrayDrivers;

public void printArray() {
    for (int i = 0; i < arrayDrivers.length; i++) {
        Driver d = arrayDrivers[i];

        System.out.println("Username : " + d.getUsername());
        System.out.println("Password : " + d.getPassword());
        System.out.println(" Manager Check: " + d.getManagerCheck());

    }
}

the from the test class you can do:

public void execute() {
    DepotDatabase  ddb = new DepotDatabase();
    ddb.printArray();
}

Upvotes: 2

If you plan to print your array in another class you show create an accessor to it.

The common pattern for Java is to use "get plus name off attribute", getDrivers() you should also avoid the class name in such geter as it may changed due to application life.

public final class DepotDatabase {

    //your code

    public Driver[] getDrivers() {
       return this.arrayDrivers;
    }

}

Next question to answer is a returning the whole array is good idea. When you return it as above you loose control on it. And every one that call that method will be able to change the content of it.

To prevent this you should use so called Defensive copying

   public Driver[] getDrivers() {
      return Arrays.copyOf(arrayDrivers, arrayDrivers.length);
   }

Then person will get an copy of it an will not harm your class.

The issue with this is that consumer of your class will have to call this method every time to get fresh list of cars.

To solve this issue you may want to user the [collection framework] where instead of array you cold define:

List<Driver> drivers new ArrayList<>();

and provide the drivers as [immutable] list

  public Iterable<Driver> getDrivers() {
          return java.util.Collections.unmodifiableList(drivers);
   }

Iterable is an interface, that allow you to obtain an interator the the list consumer of class wold have possibility to traverse it. IF you wan to allow him to check that list contains some driver you can set the return type as Collection


    class Storage {

        private String items[] = new String[10];

        public String[] getItems() {
            return Arrays.copyOf(items, items.length);
        }
    }

    class Store {

        Storage storage = new Storage();

        private void printStorage() {

            String[] items = storage.getItems();

            for (String item : items) {

            }
        }
    }

Upvotes: 0

Maroun
Maroun

Reputation: 95978

That's why you'll need to have getters and setters. You should have:

public Driver[] getDrivers() {
    return arrayDrivers;
} 

and in the other class, you simply call it (and print it or whatever).

Read this tutorial.

Upvotes: 1

Related Questions