有没有更优雅的方式来处理Java中的列表? (Python VS Java) [英] Are there any more elegant ways of handling lists in Java ? (Python VS Java)

查看:144
本文介绍了有没有更优雅的方式来处理Java中的列表? (Python VS Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢用Python处理列表的方式。它做任何递归解决方案看起来容易和干净。例如,在Python中获取元素的所有排列的典型问题如下所示:

I do like the way I can treat lists in Python. It does any recursion solution to look easy and clean. For instance the typical problem of getting all the permutations of elements in a list, in Python looks like:

def permutation_recursion(numbers,sol):
    if not numbers:
        print "this is a permutation", sol
    for i in range(len(numbers)):
        permutation_recursion(numbers[:i] + numbers[i+1:], sol + [numbers[i]])

def get_permutations(numbers):
    permutation_recursion(numbers,list())

if __name__ == "__main__":
    get_permutations([1,2,3])

我喜欢通过像
数字[:i] +数字[i + 1:] 或 sol + [numbers [i]]

如果我尝试在Java中使用完全相同的代码,那么看起来像:

If I try to code exactly the same in Java, it looks like:

import java.util.ArrayList;
import java.util.Arrays;

class rec {
    static void permutation_recursion(ArrayList<Integer> numbers, ArrayList<Integer> sol) {
       if (numbers.size() == 0)
            System.out.println("permutation="+Arrays.toString(sol.toArray()));
       for(int i=0;i<numbers.size();i++) {
             int n = numbers.get(i);

             ArrayList<Integer> remaining = new ArrayList<Integer>(numbers);
             remaining.remove(i);

             ArrayList<Integer> sol_rec = new ArrayList<Integer>(sol);
             sol_rec.add(n);

             permutation_recursion(remaining,sol_rec);
       }
    }
    static void get_permutation(ArrayList<Integer> numbers) {
        permutation_recursion(numbers,new ArrayList<Integer>());
    }
    public static void main(String args[]) {
        Integer[] numbers = {1,2,3};
        get_permutation(new ArrayList<Integer>(Arrays.asList(numbers)));
    }
}

要创建相同的递归,我需要做:

To create the same recursion I need to do :

ArrayList<Integer> remaining = new ArrayList<Integer>(numbers);
remaining.remove(i);

ArrayList<Integer> sol_rec = new ArrayList<Integer>(sol);
sol_rec.add(n);

这对于更复杂的解决方案而言非常难看。就像这个例子一样

Which is quite ugly and it gets worse for more complex solutions. Like in this example

所以我的问题是...... Java API中是否有任何内置运算符或辅助函数会使这个解决方案更Pythonic?

So my question is ... are there any buil-in operators or helper functions in the Java API that would make this solution more "Pythonic" ?

推荐答案

不。

但这就是Martin Odersky创建 Scala 的原因。他甚至说他对Scala的目标之一就是它是Java世界的Python。 Scala编译为Java字节码,并且可以轻松地与Java编译类进行交互。

But this is why Martin Odersky created Scala. He's even said that one of his goals for Scala is that it be the Python of the Java world. Scala compiles to Java bytecode and easily interops with Java compiled classes.

如果这不是一个选项,你可以看一下 Commons Collection Library

If that's not an option, you could take a look at the Commons Collection Library.

这篇关于有没有更优雅的方式来处理Java中的列表? (Python VS Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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