Chcę, aby mój tablica pominąć puste indeksów. Jest to format pliku tekstowego.
some text
1
2
3
4
2
5
4
6
5
7
6
8
Tu jest mój kodu
import java.io.*;
import java.util.*;
import java.io.IOException;
class ArrayPractice {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new FileReader(file.txt)); //reads the file
ArrayList<String> list = new ArrayList<String>(); //creates arraylist
String line;
while ((line = br.readLine()) !=null){
//i thought this just reads the lines and if its null skips it but its not happening
list.add(line);
}
br.close();
System.out.println(list);
if (list.get(2).equals(list.get(5))){ //comparing two of the lines that should be equal
System.out.println(Should be equal);
} else {
System.out.println(list.get(3));
System.out.println(list.get(7));
System.out.println(This is what is actually equal from my code)
}
System.out.println(list.size()); //says the size of the list is 16
}
}
Myślałem tak skonfigurować, jak mój kod dodaje linie ignoruje puste nich. zastanawiałem się też, dlaczego wielkość liście jest 16, gdy istnieje tylko 15 indeksy. Moje pytanie brzmi jak mogę zignorować puste wiersze tekstu w moim pliku.