我有返回数组的问题。
package iterators;
import java.util.*;
import java.io.*;
public class Nic {
String[] abel() throws IOException {
int i = 0;
BufferedReader wczytaj = new BufferedReader(new FileReader("filmy.txt"));
StringBuilder sb = new StringBuilder();
String tekst = wczytaj.readLine();
while (tekst != null) {
sb.append(tekst);
sb.append("\n");
tekst = wczytaj.readLine();
}
String calosc = sb.toString();
String film = calosc;
String znak = ",";
String[] tab;
tab = film.split(znak);
for (i = 0; i < tab.length; i++) {
System.out.println("listusia: " + i + " " + tab[i]);
}
List<String> list = Arrays.asList(tab);
Collections.reverse(list);
tab = (String[]) list.toArray();
for (i = 0; i < tab.length; i++) {
System.out.println(tab[i]);
}
return tab;
}
}
我想返回一个数组,在这里打印:
for(i=0;i<tab.length;i++){
System.out.println(tab[i]);
}
但我有问题。无法返回 tab[i],因为它给我一个错误“不兼容的类型”。我需要这个数组来进行其他操作。有人可以帮我吗?
这是我更改后的代码:
package iterators;
import java.util.*;
import java.io.*;
public class Nic{
String abel() throws IOException{
int i=0;
BufferedReader wczytaj = new BufferedReader(new FileReader("filmy.txt"));
StringBuilder sb=new StringBuilder();
String tekst=wczytaj.readLine();
while (tekst!=null) {
sb.append(tekst);
sb.append("\n");
tekst=wczytaj.readLine();
}
String calosc=sb.toString();
String film=calosc;
String znak=",";
String[]tab=new String[301];
tab=film.split(znak);
for(i=0;i<tab.length;i++){
//System.out.println("listusia: "+i+" "+tab[i]);
}
List<String> list=Arrays.asList(tab);
Collections.reverse(list);
tab = (String[]) list.toArray();
for(i=0;i<tab.length;i++){
//System.out.println(tab[i]);
}
return tab[i];
}
}