2017-01-20 3 views
0

私はコーディングの世界では新しく、問題があります。コードは配列の最初の要素で "null"と読みとります

私は、配列から文字列を読み込むシンプルなJavaクラスを作成していますが、プログラムを実行するたびに、私は非常に最初の要素に "null"を取得します。

これは私のコードです:

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; 
} 

そして、これは私のテストクラスです: パブリッククラス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); 
} 

しかし、これは私の出力である、私は非常に「ヌル」を取得します最初の要素と私は理由を知らない

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

長さ1のリストから始めるので、オブジェクト配列のデフォルト値は 'null'であり、最初の値を上書きすることはありません。 'DEFAULT_ZERO'をゼロに設定してみてください。 –

+0

代わりに代わりに 'ArrayList'を使うだけでしょうか? –

+0

さて、あなたはJavaを学ぼうとするかもしれません。 もしそうでなければ、Arrayに固執するにはCollectionやいくつかのArrayUtilsを使います。 –

答えて

0

長さが1のリストから始めるからです。

Javaで配列を作成すると、その要素はその型のデフォルト値に初期化されます。オブジェクトの場合はnullです。だから、nullを含む配列から始めましょう。

addを呼び出すと、新しい文字列がリストの最後に追加されます。要素を上書きしないので、nullは上書きされません。

DEFAULT_ZEROをゼロに設定すると、最初にアレイにnullが含まれなくなります。


手動でこのような配列のサイズを変更する代わりに、ArrayListを使用することを強く検討する必要があります。少なくとも、ArrayListのサイズ変更戦略については、スペースを使い果たしたときの長さを倍にする必要があります。毎回1ずつサイズを変更するのは非常に非効率的です。

+0

大変ありがとう、私はまだコーディングに新しく、アレイの使い方を学んでいます。次にArrayListを学ぼうとします。ありがとう – iVince905

0

temp [temp.length - 1] = nameのためです。 temp.lengthは意味2.

にすでにある

あなたはtemp[1]代わりのtemp[0]

0

他の人の答えは指摘したように、あなたはArrayListのを使うべきでnameを書きます。しかし、学習目的のためにそれを自分で作成したい場合は...

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

私の仕事を編集していただきありがとうございます、本当にありがとうございます! – iVince905

関連する問題