代码读取" null"在数组的第一个元素中

时间:2017-01-20 13:58:26

标签: java arrays 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);
}

但这是我的输出,我得到了一个&#34; null&#34;在我的第一个元素中,我不知道为什么

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

3 个答案:

答案 0 :(得分:0)

这是因为你从一个长度为1的列表开始。

在Java中创建数组时,其元素将初始化为该类型的默认值;对于对象,这是空的。因此,您从包含null的数组开始。

当您致电add时,您将新字符串附加到列表的末尾;但是你永远不会覆盖元素,因此null不会被覆盖。

DEFAULT_ZERO设置为零,并且您最初不会在数组中拥有此null

您应该强烈考虑使用ArrayList而不是像这样手动调整数组大小。至少,你应该阅读ArrayList调整大小的策略,当你的空间不足时,这个策略会增加一倍。每次调整大小为1是非常低效的。

答案 1 :(得分:0)

那是因为你这样做     temp [temp.length - 1] = name;

temp.length已经在2。

这意味着您在name而不是temp[1]

中撰写temp[0]

答案 2 :(得分:0)

正如其他人的回答所指出的那样,你应该使用ArrayList。但是如果你想为了学习目的自己建造......

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