Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.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中,将类称为ArrayList有哪些优势?_Java_Object_Arraylist - Fatal编程技术网

在Java中,将类称为ArrayList有哪些优势?

在Java中,将类称为ArrayList有哪些优势?,java,object,arraylist,Java,Object,Arraylist,假设: 如果我将我的一个类调用到我的Main方法,有多少种方法可以使用objectarraylist使列表变得有用 例如: 任何有助于消除混乱的都将不胜感激。如果你只需要计算一下你能做的账户数量,那就谢谢你了 int size = listOfAccounts.size(); List<Integer> numberOfAccounts = listOfAccounts.stream() .ma

假设: 如果我将我的一个类调用到我的Main方法,有多少种方法可以使用objectarraylist使列表变得有用

例如:


任何有助于消除混乱的都将不胜感激。如果你只需要计算一下你能做的账户数量,那就谢谢你了

int size = listOfAccounts.size();
List<Integer> numberOfAccounts = listOfAccounts.stream()
                                            .map(a -> a.getNum())
                                            .collect(Collectors.toList());
如果你想从这些帐户中提取一个数字列表,你可以这样做

int size = listOfAccounts.size();
List<Integer> numberOfAccounts = listOfAccounts.stream()
                                            .map(a -> a.getNum())
                                            .collect(Collectors.toList());

或者使用t作为流

numberOfAccounts.stream()
              .forEach(System.out::println);
如何使用findMaximum(ListOfcounts)?因为listOfAccounts是对象,而不是特定类型的arrayList

如果您只需要帐户某个字段的最大值,则无需首先提取
列表

List<Account> accounts = ...
OptionalInt max = accounts.stream()
        .mapToInt(Account::getNum)
        .max();
列出帐户=。。。
optionant max=accounts.stream()
.mapToInt(帐户::getNum)
.max();

如果要查找列表中的项目数,可以使用
myList.size()
。除此之外,我不知道你在问什么。好吧,countAccounts方法是一个愚蠢的例子!我想说也许findMaximum(ArrayList max)。如何使用findMaximum(ListOfcounts)?因为listOfAccounts是对象,而不是特定类型的arrayList@Khelwood问题是:两者(int和iteration)都给了我一个错误“Account不能转换为int”。:(@M.Harun Correct,这就是为什么我说你需要将帐户列表转换为整数列表,并给了你一个如何转换的示例。让我在回答中更清楚地说明这一点……非常感谢@Peter Lawrey
for(int i : numberOfAccounts) {
  // iterate over the list
numberOfAccounts.stream()
              .forEach(System.out::println);
List<Account> accounts = ...
OptionalInt max = accounts.stream()
        .mapToInt(Account::getNum)
        .max();