Java 在 ArrayList 中查找最大的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32593820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding Largest String in ArrayList
提问by
// Im trying to find the largest String in my ArrayList and print it out and also to include what index the largest element resides at and to print that to screen too. Im just wondering where Im going wrong.
// 我试图在我的 ArrayList 中找到最大的字符串并将其打印出来,还包括最大元素所在的索引并将其打印到屏幕上。我只是想知道我哪里出错了。
Thanks.
谢谢。
import java.util.Scanner;
import java.util.ArrayList;
public class ArraylistString
{
public static void main(String [] args)
{
// Instance of Scanner class
Scanner keyboardIn = new Scanner(System.in);
// Declare an array list of Strings
ArrayList<String> Str = new ArrayList<>();
// Add names to ArrayList
Str.add("Jim Bob");
Str.add("Bobby Jones");
Str.add("Rob Stiles");
int largestString = Str.size();
int index = 0;
// Use for loop to print out elements from ArrayList
for(int i = 0; i < Str.size(); i++)
{ // Test which String is the largest
if(Str[i].size() > largestString)
{
largestString = Str[i].size();
index = i;
}
}
// Output largest String and index it was found at
System.out.println("Index " + index + " "+ Str[index] + " " + "is the largest and is size " + largestString);
}
}
采纳答案by ABHISHEK RANA
Please try these code . Here i am trying with get() to access the ArrayList elements, which is working correctly.
请尝试这些代码。在这里,我正在尝试使用 get() 访问 ArrayList 元素,该元素工作正常。
import java.util.Scanner;
import java.util.ArrayList;
class ArraylistString
{
public static void main(String args[])
{
ArrayList<String> Str = new ArrayList<String>();
Str.add("Jim Bob");
Str.add("Bobby Jones");
Str.add("Rob Stiles");
int largestString = Str.get(0).length();
int index = 0;
for(int i = 0; i < Str.size(); i++)
{
if(Str.get(i).length() > largestString)
{
largestString = Str.get(i).length();
index = i;
}
}
System.out.println("Index " + index + " "+ Str.get(index) + " " + "is the largest and is size " + largestString);
}
}
回答by R Nar
What are you getting as output?
你得到什么作为输出?
also, the line
此外,该行
int largestString = Str.size()
is setting largestString as the number of elements in the array Str so that may cause errors. I would set it to 0 or even -1 as a baseline, or maybe Str[0].size() so that you can start your for loop with your first element as your baseline.
将最大字符串设置为数组 Str 中的元素数,这样可能会导致错误。我会将它设置为 0 甚至 -1 作为基线,或者 Str[0].size() 以便您可以将第一个元素作为基线开始 for 循环。
EDIT:
编辑:
I didn't even realize this was Java so like what people are saying, you cannot use the [] operator as you can in most languages and you should use string.length() not str.size()
我什至没有意识到这是 Java,所以就像人们所说的那样,您不能像在大多数语言中那样使用 [] 运算符,您应该使用 string.length() 而不是 str.size()
回答by Mureinik
You have the correct idea, but wrong syntax. In Java, only arrays support the []
syntax. An ArrayList
isn't an array, it's a class that implements the List
interface, and you should use the get
method to access its members. Similarly, a String
doesn't have a size()
method, it has a length()
method.
您有正确的想法,但语法错误。在 Java 中,只有数组支持该[]
语法。AnArrayList
不是数组,它是一个实现List
接口的类,您应该使用该get
方法来访问其成员。同样,aString
没有size()
方法,它有一个length()
方法。
回答by Evan LaHurd
I would set your largestString
variable to your first String
that you add:
我会将您的largestString
变量设置为您String
添加的第一个变量:
int largestString = Str.get(0).length();
Then you should use the following to check for the largest String
:
然后你应该使用以下来检查最大的String
:
if(Str.get(i).length() > largestString) {
largestString = Str.get(i).length();
index = i;
}
You cannot index into an ArrayList
with [] as you were trying to do.
你不能ArrayList
像你试图做的那样用 []索引。
I would also suggest better variable names. If I saw Str
as a variable I would think it was a String
. Maybe try strList
or something like that.
我还建议更好的变量名称。如果我将其Str
视为一个变量,我会认为它是一个String
. 也许尝试strList
或类似的东西。
回答by Johnny Willer
You can also use java.util.Collections.max
.
您也可以使用java.util.Collections.max
.
Java 8
爪哇 8
String max = Collections.max(Str, Comparator.comparing(String::length)); // or s -> s.length()
Prior Java 8
之前的 Java 8
String max = Collections.max(Str, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.length() - o2.length();
}
});
Then
然后
System.out.println("Index " + arr.indexOf(max) + " " + max + " " + "is the largest and is size " + max.length());
回答by Vishwa Ratna
From Java-8 and onwards:
从 Java-8 开始:
List<Integer> numList = Arrays.stream(Str).map(String::length).collect(Collectors.toList());
Integer m = numList.stream().mapToInt(i->i).max().orElse(4000); //get strings with their length
int k = numList.indexOf(m); //index of String with Maximum Length
System.out.println(Str.get(k)); //get your longest string