I'm pretty new to the world of coding and I have a problem.
I'm creating a simple java class that reads strings from an array but every time I run the program, I get a "null" in my very first element.
This is my 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;
}
And this is my test class: 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);
}
But this is my output, I get a "null" in my very first element and I don't know why
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
It's because you start with a list of length 1.
When you create an array in Java, its elements are initialized to the default value for the type; for objects, that's null. So, you start off with an array containing null
.
When you call add
, you append the new string to the end of the list; but you never overwrite elements, so that null
is not overwritten.
Set DEFAULT_ZERO
to zero, and you won't have this null
in the array initially.
You should strongly consider using an ArrayList
instead of manually resizing the array like this. At the very least, you should read about ArrayList
's resizing strategy, which is to double in length when you run out of space. Resizing by 1 each time is very inefficient.
That is because you do temp[temp.length - 1] = name;
Where temp.length
is already at 2.
Which means you write name
in temp[1]
instead of temp[0]
As others answers pointed, you should use ArrayList. But if you want to build it yourself for learning purpose...
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;
}
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.