Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否可以检查是否调用了函数?_Java_File_Outputstream - Fatal编程技术网

Java 是否可以检查是否调用了函数?

Java 是否可以检查是否调用了函数?,java,file,outputstream,Java,File,Outputstream,我有一个对象的ArrayList,我需要使用两个属性(使用比较器)对其进行排序。我需要将排序后的输出保存到具有不同名称的文本文件中,具体取决于用于排序的属性。例如,如果列表按attribute1排序,则文件将为attribute1.txt,如果attribute2则文件将为attribute2.txt 我希望它如何工作(伪代码): 这可能吗? 谢谢你的建议。 谢谢 伺服这里有一个面向对象的方法来解决这个需求 对列表及其排序属性使用包装器: public class ListSorter<V

我有一个对象的
ArrayList
,我需要使用两个属性(使用比较器)对其进行排序。我需要将排序后的输出保存到具有不同名称的文本文件中,具体取决于用于排序的属性。例如,如果列表按
attribute1
排序,则文件将为
attribute1.txt
,如果
attribute2
则文件将为
attribute2.txt

我希望它如何工作(伪代码):

这可能吗? 谢谢你的建议。 谢谢


伺服

这里有一个面向对象的方法来解决这个需求

对列表及其排序属性使用包装器:

public class ListSorter<V> {

    private final List<V> values;
    private String sortingAttribute;

    public ListSorter(List<V> values) {
        this.values = values;
    }

    public void sort(AttributeComparator<V> comparator) {
        Collections.sort(values, comparator);
        sortingAttribute = comparator.getSortingAttribute();
    }

    public String getSortingAttribute() {
        return sortingAttribute;
    }
}
公共类列表分类器{
私有最终列表值;
私有字符串排序属性;
公共列表分类器(列表值){
这个值=值;
}
public void排序(AttributeComparator比较程序){
集合。排序(值、比较器);
sortingAttribute=comparator.getSortingAttribute();
}
公共字符串getSortingAttribute(){
返回排序属性;
}
}
扩展Comparator接口,以便获得属性名称:

public interface AttributeComparator<T> extends Comparator<T> {
    public String getSortingAttribute();
}
public接口AttributeComparator扩展了Comparator{
公共字符串getSortingAttribute();
}
创建自定义属性比较程序,如下所示:

public class FooBarComparator implements AttributeComparator<Foo> {

    public int compare(Foo foo1, Foo foo2) {
        // skipped nullchecks for brevity
        return foo1.getBar().compare(foo2.getBar());
    }

    public String getSortingAttribute() {
        return "bar";
    }
public类FooBarComparator实现AttributeComparator{
公共int比较(Foo-foo1,Foo-foo2){
//为简洁起见跳过了空检查
返回foo1.getBar().compare(foo2.getBar());
}
公共字符串getSortingAttribute(){
返回“bar”;
}
}

使用:

List yourList=new ArrayList();
ListSorter示例=新的ListSorter(您的列表);
AttributeComparator comparator1=新的FooBarComparator();
排序(比较器1);
FileWriter fwstream=新的FileWriter(例如.getSortingAttribute()+“.txt”);

是的,这是可能的。您必须在if语句之外定义FileWriter fwstream,以便在if后面的代码中使用它。您能否向我们提供更多的上下文,例如排序是如何完成的,您不能在使用
attribute1
attribute2
排序时仅设置变量?像
isAttribute1=false
意味着调用的是
attribute2
。或者一根绳子。。我们需要更多的信息。这条线太多了。但是,Gilbert Le Blanc有一个很好的观点,谢谢。
public class FooBarComparator implements AttributeComparator<Foo> {

    public int compare(Foo foo1, Foo foo2) {
        // skipped nullchecks for brevity
        return foo1.getBar().compare(foo2.getBar());
    }

    public String getSortingAttribute() {
        return "bar";
    }
List<Foo> yourList = new ArrayList<Foo>();
ListSorter<Foo> example = new ListSorter<Foo>(yourList);
AttributeComparator comparator1 = new FooBarComparator();
example.sort(comparator1);
FileWriter fwstream = new FileWriter(example.getSortingAttribute() +".txt");