[英]Compare String elements in an Array
我正在嘗試比較數組中的元素。 例如,
labels = ["abc1","abc2","abc3","abc4"]
我想采用具有最高值的字符串。 在這種情況下,它的 abc4。 我對編碼很陌生,所以如果有人可以幫助我理解邏輯,那就太好了。
您正在尋找的是一種將每個字符串相互比較的方法。
Java 有一個內置的方法來為String
class 執行此操作,稱為compareTo
方法。 顧名思義,它將一個字符串與另一個字符串進行比較。
String a = "abc1";
String b = "abc2";
String c = "abc1";
System.out.println(a.compareTo(b)); // prints a negative number because `a` is smaller than `b`
System.out.println(b.compareTo(a)); // prints a positive number because `b` is bigger than `a`
System.out.println(c.compareTo(a)); // prints 0 because `a` and `b` have the same letters.
有關compareTo
方法,請參閱官方 java 文檔:
[返回]如果參數字符串等於該字符串,則值為 0; 如果此字符串按字典順序小於字符串參數,則值小於 0; 如果此字符串按字典順序大於字符串參數,則值大於 0。
您可以在示例中使用它的方式是:
String biggest = labels[0];
for(int i = 1; i < labels.length; i++){
if(biggest.compareTo(labels[i]) < 0) biggest = labels[i];
}
System.out.println(biggest);
注意:有關此方法如何選擇“更大”的更多詳細信息,請參閱 java 文檔(上面鏈接)。 如果您有自己的規則來決定哪個更大,那么您可以制定自己的方法來定義它。
更新:例如,請參閱 XtremeBaumer 的評論
"abc20".compareTo("abc100") = 1。表示 abc20 大於 abc100,因此 compareTo() 不一定對任務有用
您的問題需要改進,根據您所說的,讓我們從字符串中刪除所有 abc,獲取最大 integer,然后返回或打印"abc" concatenated to the max number
:
import java.util.Arrays;
import java.util.stream.IntStream;
public class Solution {
public static void main(String[] args) throws Throwable {
int numberOfElements = 100;
String[] labels = new String[numberOfElements];
Arrays.setAll(labels, element -> "abc" + element);
int max = Arrays.stream(labels).mapToInt(element -> Integer.parseInt(element.substring(3))).max().getAsInt();
System.out.println(String.join(" | ", labels));
System.out.println();
System.out.println();
System.out.println("The max here is : ");
System.out.println("abc" + max);
}
}
Output 這里:
abc0 | | abc1 | | abc2 | | abc3 | | abc4 ...... || abc99
The max here is :
abc99
嘗試這樣的事情:
var strings = new ArrayList<String>();
strings.add("abc1");
strings.add("abc2");
strings.add("abc3");
strings.add("abc4");
var integers = strings
.stream()
.map(string -> Integer.valueOf(string.replaceAll("[^0-9]+", "")))
.collect(Collectors.toList());
var max = Collections.max(integers);
var indexMax = integers.indexOf(max);
var maxString = strings.get(indexMax);
System.out.println(maxString);
更簡單的方法: @mcieciel已經發布了這個。
List<String> list = Arrays.asList("abc1", "abc22", "abc33", "abc19");
List<Integer> intList = list.stream()
.map(s -> Integer.parseInt(s.replaceAll("[^0-9]+", "")))
.collect(Collectors.toList());
int index = intList.indexOf(Collections.max(intList));
System.out.println(list.get(index));
另一種方法是創建一個map
,它將在鍵值對中包含字符串及其對應的 integer 值。然后從 map 中找到帶有其鍵的最大值。
這可能是矯枉過正。
String key = list.stream()
.collect(Collectors.toMap( // creating map like this format : abc1->1 , abc22->22 ...
Function.identity(), // key of the map i.e the string value
s -> Integer.parseInt(s.replaceAll("[^0-9]+", "")), // integer value
(e1, e2) -> e1)) // if multiple same entry exists choose one
.entrySet() // now we have map, we can iterate and find out the key which holds max value
.stream()
.max((e1, e2) -> Integer.compare(e1.getValue(), e2.getValue())) // comparing values
.get()
.getKey();
System.out.println(key);
注意:如果您有沒有任何數字的字符串值,則兩者都不起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.