[英]Code reads “null” in the first element of the array
我对编码世界还很陌生,但我遇到了问题。
我正在创建一个简单的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
这是因为您从长度为1的列表开始。
当您使用Java创建数组时,其元素将初始化为该类型的默认值; 对于对象,则为null。 因此,从包含null
的数组开始。
调用add
,会将新字符串追加到列表的末尾; 但是您绝不会覆盖元素,因此null
不会被覆盖。
将DEFAULT_ZERO
设置为零,并且最初在数组中不会包含此null
。
您应该强烈考虑使用ArrayList
而不是像这样手动调整数组的大小。 至少,您应该了解ArrayList
的大小调整策略,该策略在空间不足时将长度加倍。 每次将大小调整为1时效率很低。
那是因为你做temp [temp.length-1] = name;
其中temp.length
已经为2。
这意味着您用temp[1]
而不是temp[0]
写name
正如其他答案指出的那样,您应该使用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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.