返回/打印数组

时间:2013-03-02 19:41:38

标签: java arrays return

我有一个返回数组的问题。

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],因为它会给我一个错误&#34;不兼容的类型&#34;。我需要这个数组到其他操作。有谁可以帮助我?

这是我更改后的代码:

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

2 个答案:

答案 0 :(得分: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]);
            }

答案 1 :(得分:0)

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

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

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();
    }

}