简体   繁体   English

打印两个ArrayLists对应的item?

[英]Printing the corresponding item of two ArrayLists?

[1] I want to print out the highest value from the second ArrayList (300) with its corresponding name from the first ArrayList (in this case, Abraham Lincoln) [1] 我想打印出第二个 ArrayList (300) 中的最高值以及第一个 ArrayList 中的相应名称(在本例中为 Abraham Lincoln)

Ex.前任。 "Abraham Lincoln 300" “亚伯拉罕·林肯 300”

With the code I've already written, the entirety of the first ArrayList and the highest value in the second ArrayList will print.使用我已经编写的代码,将打印第一个 ArrayList 的全部内容和第二个 ArrayList 中的最高值。

How could I go about this?我怎么能go这个呢? I tried using an index method but I was unsure how to properly apply it.我尝试使用索引方法,但不确定如何正确应用它。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Names {
    public static void main(String args[]){

ArrayList<String> students = new ArrayList<>();
students.add("John F. Kennedy");
students.add("Donald Trump");
students.add("Theodore Roosevelt");
students.add("Abraham Lincoln");
students.add("Millard Fillmore");

System.out.println(students);

ArrayList<Integer> scores = new ArrayList<>();
scores.add(100);
scores.add(250);
scores.add(120);
scores.add(300);
scores.add(200);

Integer max = Collections.max(scores);
System.out.println(max); 
    }
}

First find the element with the highest value.首先找到具有最高值的元素。 Remember the index at which you found that value.记住您找到该值的索引。

Then print from both arraylists the element with the index you remembered before.然后从两个数组列表中打印具有您之前记住的索引的元素。

With the line随着线

Integer max = Collections.max(scores);

you get the maximum value easily, but you do not know at which index it occurred.您很容易获得最大值,但您不知道它出现在哪个索引处。 Find another way to get the max, since actually you are interested in the index, not the value.寻找另一种获得最大值的方法,因为实际上您对索引而不是值感兴趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM