Quick actions

cmd+k|ctrl+k

Navigation

Languages

Command

Snippet info

Language

Java

Visibility

public

Author

yoneda

Created

2018-01-02T07:54:00Z

Updated

2018-01-02T08:02:03Z

interface Command{
    public void process(int[] args);
}
class PrintCommand implements Command{
    public void process(int[] args){
        System.out.print("elements: ");
        for(Integer i : args){
            System.out.print(i + " ");
        }
      System.out.println();
    }
}
class SumCommand implements Command{
    public void process(int[] args){
        long sum = 0;
        for(Integer i : args){
            sum += i;
        }
        System.out.println("sum: " + sum);
    }
}
class Worker{
    public void process(int[] args, Command command){
        command.process(args);
    }
}
class Main {
    public static void main(String[] args) {
        Worker worker = new Worker();
        worker.process(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, new PrintCommand());
        worker.process(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, new SumCommand());
        worker.process(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, new Command(){
            public void process(int[] args){
                long sum = 0;
                for(Integer i : args){
                    sum += i;
                }
                System.out.println("average: " + (sum / args.length));
            }
        });
        worker.process(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, (args1) -> {
            long sum = 0;
            for(Integer i : args1){
                sum += i;
            }
            System.out.println("average: " + (sum / args1.length));
        });
    }
}
INFO