javac String / Object中不兼容的类型

时间:2015-12-14 15:14:50

标签: java generics javac

我收到此错误

:/tmp$ javac DrawPowerSet.java 
DrawPowerSet.java:38: error: incompatible types
        String value = set.get(i);
                              ^
  required: String
  found:    Object
1 error

当我尝试编译这个小代码时:

我在ubuntu 15.10版上发表了

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/* @author Philippe Fournier-Viger, 2014 */
public class DrawPowerSet {
    public static void main(String[] arg) throws IOException,
            InterruptedException {
        String[] set = new String[]{"a", "b", "c", "d", "e"};
        String output = "input.dot";
        BufferedWriter writer = new BufferedWriter(new FileWriter(output));
        writer.write("digraph mygraph{");
        for (long i = 0, max = 1 << set.length; i < max; i++) {
            List newSet = new ArrayList();
            for (int j = 0; j < set.length; j++) {
                int isSet = (int) i & (1 << j);
                if (isSet > 0) {
                    newSet.add(set[j]);
                }
            }
            if (newSet.size() != set.length) {
                printLinksToImmediateSupersets(newSet, set, writer);
            }
        }
        writer.write("}");
        writer.close();
    }
    private static void printLinksToImmediateSupersets(List subset,
            String[] set, BufferedWriter writer) throws IOException {
        for (int i = 0; i < set.length; i++) {
            String value = set[i];
            if (subset.contains(value) == false) {
                List newSet = new ArrayList();
                newSet.addAll(subset);
                newSet.add(value);
                writer.write(asString(subset) + " -> " + asString(newSet)
                        + " \n");
            }
        }
    }
    private static String asString(List set) {
        Collections.sort(set);
        if (set.size() == 0) {
            return "\"{}\"";
        }
        StringBuffer buffer = new StringBuffer();
        buffer.append("\"{");
        for (int i = 0; i < set.size(); i++) {
            String value = set.get(i);
            buffer.append(value);
            if (i != set.size() - 1) {
                buffer.append(",");
            }
        }
        buffer.append("}\"");
        return buffer.toString();
    }
}

java -version

  • java版本&#34; 1.7.0_91&#34;
  • OpenJDK运行时环境(IcedTea 2.6.3)(7u91-2.6.3-0ubuntu0.15.10.1)
  • OpenJDK 64位服务器VM(内置24.91-b01,混合模式)

0 个答案:

没有答案