第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java比較arraylist中數(shù)組的元素

Java比較arraylist中數(shù)組的元素

繁華開滿天機(jī) 2022-07-14 17:35:11
我正在嘗試將輸入與數(shù)組列表中的值進(jìn)行比較。public compare(number)前任; 我有一個數(shù)組列表:[100,1102,14051,1024, / 142,1450,15121,1482,/ 141,1912,14924,1001] // the / represents each entry數(shù)組的每個索引都代表我程序中的一個唯一屬性。例如索引 0 表示user id,索引 1 表示room number等。如果我這樣做arraylist.get(2),它會返回第二個數(shù)組(142,1450,15121,1482)。我正在嘗試與number每個數(shù)組中的第二個元素進(jìn)行比較。所以說我運(yùn)行這個compare(1102),我希望它遍歷每個數(shù)組中的每個 [1],如果該索引處有匹配項,則返回 true。所以我希望它比較“數(shù)字”(1102)和每個第一個索引元素(1102,1450,1912),因為 1102 在數(shù)組列表中,所以返回 true我一直在四處尋找,但找不到如何實現(xiàn)這一點,或者以正確的方式提出問題
查看完整描述

3 回答

?
慕哥9229398

TA貢獻(xiàn)1877條經(jīng)驗 獲得超6個贊

Stream API 可以做到這一點。


public class MyCompare 

{

    private static Optional<Integer> compare(Integer valueToCompare)

    {

        Optional<Integer[]> matchingArray = listToCompare.stream()

                .filter(eachArray -> eachArray[1].equals(valueToCompare))

                .findFirst();


        if(matchingArray.isPresent())

        {

            for(int i = 0; i < listToCompare.size(); i++)

            {

                if(listToCompare.get(i).equals(matchingArray.get()))

                {

                    return Optional.of(i);

                }

            }

        }


        return Optional.empty();

    }


    private static List<Integer[]> listToCompare = new ArrayList<>();


    public static void main(String[] args)

    {

        listToCompare.add(new Integer[] { 100, 1102, 14051, 1024 });

        listToCompare.add(new Integer[] { 142, 1450, 15121, 1482 });

        listToCompare.add(new Integer[] { 141, 1912, 14924, 1001 });


        Optional<Integer> listIndex = compare(1102);

        if(listIndex.isPresent())

        {

            System.out.println("Match found in list index " + listIndex.get());

        }

        else

        {

            System.out.println("No match found");

        }

    }

}

在列表索引 0 中找到匹配項


查看完整回答
反對 回復(fù) 2022-07-14
?
寶慕林4294392

TA貢獻(xiàn)2021條經(jīng)驗 獲得超8個贊

遍歷您的列表并每隔一個元素進(jìn)行比較:


public static boolean compare (int number){

    for( int i = 0; i<arraylist.size(); i++){

         if(number == arraylist.get(i)[1]){

            return true;

          }

     }

    return false;

 }


查看完整回答
反對 回復(fù) 2022-07-14
?
一只甜甜圈

TA貢獻(xiàn)1836條經(jīng)驗 獲得超5個贊

直接的方法是使用增強(qiáng)的 for 循環(huán):


for (int[] items : list) {

    // do your checking in here

}

更高級但更優(yōu)雅的方法是使用流:


list.stream()       // converts the list into a Stream.

    .flatMap(...)   // can map each array in the list to its nth element.

    .anyMatch(...); // returns true if any value in the stream matches the Predicate passed.

流 API:https ://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html


查看完整回答
反對 回復(fù) 2022-07-14
  • 3 回答
  • 0 關(guān)注
  • 195 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號