Java – Array di oggetti
Sintassi
Un array di oggetti è un array di tipo riferimento . La sintassi per dichiarare una variabile in grado di contenere un riferimento ad un array è la seguente
String[] names;
In Java gli array sono oggetti e si creano con l’operatore new, quindi per dichiarare un array di 10 oggetti String usa il seguente codice
String[] names = new String[10];
Esempio: array di mesi
Poniamo di avere un array di oggetti dove ogni oggetto è un mese dell’anno, la classe Mese è la seguente
public class Mese {
private String nome;
private int giorni;
public Mese(String n, int g) {
nome = n;
giorni = g;
}
public String getNome(){
return nome;
}
public int getGiorni(){
return giorni;
}
}
Il costruttore riceverà solo il nome del mese ed il suo numero di giorni, come unici metodi avremo il getNome()
e il getGiorni()
che restituiscono rispettivamente il nome del mese e il numero di giorni.
Ora creiamo la classe Mesi che conterrà l’array di oggetti Mese
package spacecoding.array.oggetti;
public class Mesi {
private Mese[] mesi = new Mese[12];
public Mesi() {
mesi[0] = new Mese("gennaio",31);
mesi[1] = new Mese("febbraio",28);
mesi[2] = new Mese("marzo",31);
mesi[3] = new Mese("aprile",30);
mesi[4] = new Mese("maggio",31);
mesi[5] = new Mese("giugno",30);
mesi[6] = new Mese("luglio",31);
mesi[7] = new Mese("agosto",31);
mesi[8] = new Mese("settembre",30);
mesi[9] = new Mese("ottobre",31);
mesi[10] = new Mese("novembre",30);
mesi[11] = new Mese("dicembre",31);
}
public Mese getDati(int i) {
return mesi[i-1];
}
public String getMese(int i) {
return mesi[i-1].getNome();
}
public int getGiorni(int i){
return (mesi[i-1]).getGiorni();
}
}
La classe Mesi inizializza l’array di oggetti Mese con i relativi dati, creiamo anche tre metodi: getDati(int i)
che restituisce un oggetto di tipo Mese, getMese(int i)
che restituisce un oggetto di tipo String che è il nome del mese, getGiorni(int i)
che restituisce un intero che è il numero di giorni presenti in quel mese. Nota che il parametro formale è un numero intero che verrà chiesto all’utente e rappresenta il mese da 1 a 12 , quindi nei return
dei metodi sottraggo mesi[i-1]
siccome abbiamo i mesi nell’array da 0 a 11.
Quindi testiamo le classi con un semplice main
package spacecoding.array.oggetti;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
InputStreamReader in= new InputStreamReader(System.in);
BufferedReader keyboard=new BufferedReader(in);
Mesi mesi = new Mesi();
int numeroMese;
try{
System.out.println("Inserisci il numero di un mese (da 1 a 12)");
numeroMese=Integer.parseInt(keyboard.readLine());
System.out.println("Mese: "+ mesi.getMese(numeroMese)+ " ed ha "+mesi.getGiorni(numeroMese)+" giorni");
}catch(IOException e){
}
}
}
Esempio: classe triangolo
Creo una classe triangolo che ha come attributo un array di oggetti Point
classe Point.java
package spacecoding.geometry;
public class Point {
private double x;
private double y;
private String name;
public Point(double x, double y, String name){
this.x=x;
this.y=y;
this.name=name;
}
public Point(){
x=0;
y=0;
name="O";
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
", name='" + name + '\'' +
'}';
}
}
classe Triangle.java
package spacecoding.geometry;
public class Triangle {
Point[] points=new Point[3];
public Triangle(Point p1,Point p2, Point p3){
points[0]=p1;
points[1]=p2;
points[2]=p3;
}
public Triangle(Point[] points){
this.points=points;
}
}
La classe ha due costruttori, il primo riceve 3 oggetti Point, il secondo riceve un array di oggetti Point