使用一个接口应用方法ArrayList的 [英] Using an interface to apply method to ArrayList

查看:137
本文介绍了使用一个接口应用方法ArrayList的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检讨考试。在一个旧的测试的一个问题是:使用一个接口,编写用于任意方法将一个ArrayList的每一个元素的方法。无论是ArrayList和方法的参数的方法。

请问一个可行的解决方案是:

 公众和LT;对象>的someMethod(){
    扫描仪GLaDOS的=新(的someArray);
    someArray.useDelimiter(,);
    对于(i = 0; I< someArray.length;我++){
        someOtherMethod(的someArray [I]);
    }
}


解决方案

我会一直期待这样的事情:

  //定义界面重新present任意功能
接口功能< T> {
    无效Func键(T EL);
}//现在该方法应用一个任意函数到列表
< T>无效applyFunctionToArrayList(ArrayList的< T>列表,功能与LT; T> FUNC){
    //遍历列表
    对于(T项目:名单){
        //调用任意功能
        func.Func(项目);
    }
}


这是在这个问题含糊不清,但是这可能意味着返回新的ArrayList,所以这可能是另一种可以接受的答案

  //定义界面重新present任意功能
接口功能< T> {
    ŧFunc键(T EL);
}//现在该方法应用一个任意函数到列表
< T> ArrayList的< T> applyFunctionToArrayList(ArrayList的< T>列表,功能与LT; T> FUNC){
    ArrayList的< T> newList =新的ArrayList< T>();    //遍历列表
    对于(T项目:名单){
        //调用任意功能
        newList.add(func.Func(项目));
    }
}

在一个侧面说明,你会那么,也就是说,调用类的应用程序(例如,在列表中的每个数字增加一倍):

 的ArrayList<双>清单= Arrays.asList(1.0,2.0,3.0);
ArrayList的<双> doubledList = applyFunctionToArrayList(列表,
  新功能与LT;双> {
    双Func键(双X){
        返回X * 2;
    }
});

I'm reviewing for an exam. A question on an old test was to: Using an interface, write a method that applies an arbitrary method to every element of an ArrayList. Both the arraylist and method are the parameters to the method.

Would a workable solution be:

public <object> someMethod() {
    scanner GLaDOS = new (someArray);
    someArray.useDelimiter(",");
    for (i = 0; i < someArray.length; i++) {
        someOtherMethod(someArray[i]);
    }
}

解决方案

I would've expected something like this:

// define the interface to represent an arbitrary function
interface Function<T> {
    void Func(T el);
}

// now the method to apply an arbitrary function to the list
<T> void applyFunctionToArrayList(ArrayList<T> list, Function<T> func){
    // iterate over the list
    for(T item : list){
        // invoke the "arbitrary" function
        func.Func(item);
    }
}


It's ambiguous in the question, but this might mean returning a new ArrayList, so this might be another acceptable answer

// define the interface to represent an arbitrary function
interface Function<T> {
    T Func(T el);
}

// now the method to apply an arbitrary function to the list
<T> ArrayList<T> applyFunctionToArrayList(ArrayList<T> list, Function<T> func){
    ArrayList<T> newList = new ArrayList<T>();

    // iterate over the list
    for(T item : list){
        // invoke the "arbitrary" function
        newList.add(func.Func(item));
    }
}

On a side note, you would then, say, invoke the application like (for example, doubling every number in the list):

ArrayList<Double> list = Arrays.asList(1.0, 2.0, 3.0);
ArrayList<Double> doubledList = applyFunctionToArrayList(list, 
  new Function<Double>{
    Double Func(Double x){
        return x * 2;
    }
});

这篇关于使用一个接口应用方法ArrayList的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆