0

我有返回数组的问题。

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

2 回答 2

1

List#toArray()返回一个Object[]数组。改用重载的List#toArray(T[])

tab = list.toArray(new String[list.size()]);
            for(i=0;i<tab.length;i++){
                System.out.println(tab[i]);
            }
于 2013-03-02T19:43:36.897 回答
0

您收到错误是因为您的方法"Incompatible types"的返回类型是并且您正在尝试返回( )。如果您想返回,则必须将方法的返回类型更改为而不是。abel()String[]Stringtab[i]tab[i]abel()StringString[]

如果要返回整个数组,请使用String[]返回类型。它应该工作。

import java.util.*;
import java.io.*;


public class Nick {
    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;

    }

    public static void main(String[] args) throws IOException {
        Nick nick = new Nick();
        nick.abel();
    }

}
于 2013-03-02T19:56:02.580 回答