简体   繁体   中英

Converting Java Class (Array to Arraylist)

I have to convert a Java class (with his relative methods) created using Arrays into the same one but using Arraylist.

Here's the class I have to convert:

import java.util.Arrays;

public class AutoNoleggio {
    private Persona titolare;
    private String nome;
    private String indirizzo;
    private Mezzo[] IndMezzo;

    public AutoNoleggio(Persona titolare, String nome, String indirizzo, int n) {
        this.titolare = titolare;
        this.nome = nome;
        this.indirizzo = indirizzo;
        this.IndMezzo = new Mezzo[n];
        for (int i = 0; i < n; i++) {
            Persona p = new Persona("", "", 0);
            IndMezzo[i] = new Mezzo(p, 0, 0);
        }
    }

    public Persona getTitolare() {
        return titolare;
    }

    public void setTitolare(Persona titolare) {
        this.titolare = titolare;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getIndirizzo() {
        return indirizzo;
    }

    public void setIndirizzo(String indirizzo) {
        this.indirizzo = indirizzo;
    }

    public void setIndMezzo(int i, Mezzo ogg) {
        IndMezzo[i] = ogg;
    }

    public String getTitolareIndice(int indice) {
        Persona persona = IndMezzo[indice].getTitolare();
        String nome = persona.getNome();
        String cognome = persona.getCognome();
        if(nome != ""){
            return nome + " " + cognome;}
        else {
            return "*Errore: Nessun mezzo trovato all'indice scelto*";
        }
    }

    public void getIndiceTitolare(Persona titolare) {
        for (int i = 0; i < IndMezzo.length; i++) {
            if (IndMezzo[i].getTitolare() == titolare) {
                System.out.println(i);
            }
        }
    }

    public void stampaMezzi(){
        String mezzi = Arrays.toString(IndMezzo);
        System.out.println(mezzi);
    }
}

tl;dr

private List< Mezzo > indMezzo ;
        List< Mezzo > mezzos = new ArrayList<>( countMezzos ) ;
        for (int i = 0; i < countMezzos; i++) {
            Persona p = new Persona( "", "", 0 ) ;
            mezzos.add( new Mezzo( p, 0, 0 ) ) ;
        }
        this.indMezzo = List.copyOf( mezzos ) ;

Details

By the way, in Java naming conventions, a variable's name must start with a lowercase letter. So your member field private Mezzo[] IndMezzo; should be indMezzo .

Apparently you want to use an ArrayList rather than mere array Mezzo[] as the type for your member field. If so, change this:

private Mezzo[] indMezzo;

… to this:

private ArrayList< Mezzo > indMezzo ;

If your purpose would allow for other implementations of List rather than only the ArrayList class, use the more general type.

private List< Mezzo > indMezzo ;

You can assign an ArrayList object to indMezzo . And you could just as well assign a LinkedList object to indMezzo . You can assign an object of any class that implements the List interface.

Use more descriptive variable names. Something like countMezzos rather than n .

To instantiate your List object, change your code:

        this.IndMezzo = new Mezzo[n];
        for (int i = 0; i < n; i++) {
            Persona p = new Persona("", "", 0);
            IndMezzo[i] = new Mezzo(p, 0, 0);
        }

… to this:

        List< Mezzo > mezzos = new ArrayList<>() ;
        for (int i = 0; i < countMezzos; i++) {
            Persona p = new Persona( "", "", 0 ) ;
            mezzos.add( new Mezzo( p, 0, 0 ) ) ;
        }
        this.indMezzo = mezzos ;

As a slight optimization, you can specify the initial size of the list.

        List< Mezzo > mezzos = new ArrayList<>( countMezzos ) ;
        …

In many cases, it is better to use an unmodifiable list. If that suits your situation, use List.copyOf .

        List< Mezzo > mezzos = new ArrayList<>( countMezzos ) ;
        for (int i = 0; i < countMezzos; i++) {
            Persona p = new Persona( "", "", 0 ) ;
            mezzos.add( new Mezzo( p, 0, 0 ) ) ;
        }
        this.indMezzo = List.copyOf( mezzos ) ;

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