2017-01-20 3 views
0

Ich bin ziemlich neu in der Welt der Codierung und ich habe ein Problem.Code liest "Null" im ersten Element des Arrays

Ich erstelle eine einfache Java-Klasse, die Strings aus einem Array liest, aber jedes Mal, wenn ich das Programm ausführen, bekomme ich eine "Null" in meinem allerersten Element.

Dies ist mein Code:

public class Airline { 

/* Fields */ 
private String name; 
private String[] list; 
private int size = 0; 
private int DEFAULT_SIZE = 1; 

/* Constructor */ 
public Airline() { 
    list = new String[DEFAULT_SIZE] ; // creates an airline array 
} 

/* Methods */ 

// method that adds "airline name" into the array 
public void add(String name) { 

    this.name = name; 
    //a new array with + 1 index 
    String[] temp = new String[list.length + 1]; 

    //copy items from list[] to temp[] 
    for (int i = 0; i < list.length; i++) { 
     temp[i] = list[i]; 
    } 

    // add the last integer to new temp 
    temp[temp.length - 1] = name; 
    list = temp; 
} 

// method that reads from the array start 
public int read(int read) { 
    for (int i = 0; i < list.length; i ++) { 
     Airline temp = new Airline(); 
     System.out.println("Airline: " + list[i]); 
    } 
    return size; 
} 

Und das ist meine Testklasse: public class TestAirline {

public static void main(String[] args) { 

    //create the object 
    Airline airline = new Airline(); 

    // add airline names 
    airline.add("Air Canada"); 
    airline.add("West Jet"); 
    airline.add("Sunwing Airlines"); 
    airline.add("Air Transat"); 
    airline.add("Emirates"); 
    airline.add("Cathay Pacific"); 
    airline.add("Etihad"); 
    airline.add("British Airways"); 
    airline.add("Delta Airlines"); 
    airline.add("United Airlines"); 
    airline.add("American Airlines"); 
    airline.add("Porter Airlines"); 

    //read the array 
    airline.read(0); 
} 

Aber das ist meine Ausgabe ist, dass ich eine "Null" erhalten sie in meinem sehr erstes Element und ich weiß nicht, warum

Airline: null 
Airline: Air Canada 
Airline: West Jet 
Airline: Sunwing Airlines 
Airline: Air Transat 
Airline: Emirates 
Airline: Cathay Pacific 
Airline: Etihad 
Airline: British Airways 
Airline: Delta Airlines 
Airline: United Airlines 
Airline: American Airlines 
Airline: Porter Airlines 
+2

Weil Sie mit einer Liste der Länge 1 beginnen; Der Standardwert von Objekt-Arrays ist "null", und Sie überschreiben niemals den ersten Wert. Versuchen Sie, DEFAULT_ZERO auf Null zu setzen. –

+0

Und vielleicht stattdessen nur eine 'ArrayList'? –

+0

Nun, vielleicht versuchst du einfach Java zu lernen. Wenn nicht, verwenden Sie Collection oder einige ArrayUtils, wenn Sie bei Array bleiben möchten. –

Antwort

0

Es ist, weil Sie mit einer Liste der Länge 1 beginnen.

Wenn Sie ein Array in Java erstellen, werden seine Elemente mit dem Standardwert für den Typ initialisiert. Für Objekte ist das null. Sie beginnen also mit einem Array, das null enthält.

Wenn Sie add aufrufen, fügen Sie die neue Zeichenfolge an das Ende der Liste an; aber Sie überschreiben nie Elemente, so dass null nicht überschrieben wird.

Setzen Sie DEFAULT_ZERO auf Null, und Sie werden diese null im Array zunächst nicht haben.


Sie sollten in Erwägung ziehen, einen ArrayList anstelle von manuell das Array wie folgt Ändern der Größe. Zumindest sollten Sie die Größenänderungsstrategie von ArrayList lesen, die sich verdoppelt, wenn der Speicherplatz knapp wird. Die Größenänderung um jeweils 1 ist sehr ineffizient.

+0

Okay danke, ich bin noch ziemlich neu im Codieren und ich lerne nur, wie man Arrays benutzt, ich werde versuchen, ArrayList als nächstes zu lernen. Danke – iVince905

0

Das liegt daran, dass Sie temp [temp.length - 1] = name;

Wo temp.length ist bereits bei 2.

Was bedeutet, Sie schreiben name in temp[1] statt temp[0]

0

Wie andere Antworten darauf, sollten Sie Arraylist. Aber wenn Sie es für Lernzwecke selbst bauen möchten ...

public class Airline { 

    /* Fields */ 
private String name; //This is useless as you never really need it 
private String[] list; 
private int size = 0; //This is useless as you never really use it 
private int DEFAULT_SIZE = 1; //This is useless as you never really need it 

/* Constructor */ 
public Airline() { 

    // list = new String[DEFAULT_SIZE] ; 
/* The line above is useless as you are wasting space. If you want to use an array, then you should initialize it only when you want to put the first element inside. */ 

} 

/* Methods */ 

// method that adds "airline name" into the array 
public void add(String name) { 
/* The argument name already hold the "name" of the latest airline */ 
     //this.name = name; 

    //a new array with + 1 index 
//Just check if list is null here 
if(list==null) list = new String[1]; list[0] = name; 
else { 
    String[] temp = new String[list.length + 1]; 

    //copy items from list[] to temp[] 
    for (int i = 0; i < list.length-1; i++) { 
     temp[i] = list[i]; 
    } 

    // add the last integer to new temp 
    temp[temp.length - 1] = name; 
    list = temp; 
} 
} 

// method that reads from the array start 
public int read() { 
//Notice you don't need the argument read as you always read from the start, if you wanted to read from the index read, replace i=0 below by i=read and add the argument 
    for (int i = 0; i < list.length; i ++) { 
     Airline temp = new Airline(); //And as far as I know, you don't need this too 
     System.out.println("Airline: " + list[i]); 
    } 
    return size; 
} 
+0

Vielen Dank für die Bearbeitung meiner Arbeit, ich schätze es sehr! – iVince905

Verwandte Themen