Finding index of recurring objects in an ArrayList in Java

vicki :
public class NewMain {
    public static void main(String[] args) {
        ArrayList<String> array = new ArrayList<>();
        array.add("Ace of Hearts");
        array.add("King of Clubs");
        array.add("Three of Spade");
        array.add("Ace of Hearts");
        array.add("Ten of Clubs");
        for(String s: array)
            System.out.println(s+"at postion: "+array.indexOf(s));
    }
}

IndexOf() returns the first occurence of the object in an ArrayList. Assuming my code is the hand of a poker player, and I want to know the indices of the cards he is holding. Ace of Hearts is at positon 1 and 4. But IndexOf() returns as position 1 and 1 for both occurences of Ace of Hearts.

Question:
1)Is there any other way of getting the value?
2)Should I use someother Data Structure like HashMap or something else?

janith1024 :

The List.indesOf() method will return the first occurrence of the list. But your requirement can fulfill as following way.

    import java.util.ArrayList;

    public class NewMain {
        public static void main(String[] args) {
            ArrayList<String> array = new ArrayList<>();
            array.add("Ace of Hearts");
            array.add("King of Clubs");
            array.add("Three of Spade");
            array.add("Ace of Hearts");
            array.add("Ten of Clubs");
            for(int i =0;i<array.size();i++)
                System.out.println(array.get(i)+"at postion: "+i);
        }
    }

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Finding same objects assigned to a string in an ArrayList

分類Dev

Finding Recurring String in a TEXT file using PHP

分類Dev

get list of unique objects from an arraylist in java

分類Dev

Map of Objects, can't access ArrayList element by index

分類Dev

Objects to ArrayList

分類Dev

GeoDjango: Finding objects in radius

分類Dev

Finding Index of in combination with RegEx

分類Dev

Java ArrayList.remove(index)の問題

分類Dev

Jackson: Convert JSON to object: ArrayList of objects with arraylist of objects with arraylist of objects

分類Dev

java.lang.IndexOutOfBoundsException:Index:1、Size:0 at java.util.ArrayList.get(ArrayList.java:437)

分類Dev

Finding the index of a sublist in a Numpy Array

分類Dev

Finding the cell to the right of the index match

分類Dev

Finding index out of bounds exception

分類Dev

ArrayListのJava ArrayList

分類Dev

Java JDT finding methods

分類Dev

JAVA: How do I merge objects from an arrayList based on a property value?

分類Dev

finding the force needed based on distances between objects

分類Dev

Finding maximum and minimum Inside array of objects

分類Dev

finding index of key in an ordered dictionary in powershell

分類Dev

Finding the index of a date in a DateTime object python

分類Dev

Finding the index of occurrence before the last occurrence of a string

分類Dev

Finding the index or unique values from a dataframe column

分類Dev

Finding index of sublist in list python 3

分類Dev

Finding the number at a specific index of spirally filled matrix

分類Dev

Finding the column index for the first numeric or date column

分類Dev

JavaのArrayListのArrayList

分類Dev

Finding specific range of number in java

分類Dev

Out-projected type 'ArrayList<*>' prohibits the use of 'public open fun add(index: Int, element: E): Unit defined in java.util.ArrayList'

分類Dev

Create index in Mongoose for array of objects

Related 関連記事

  1. 1

    Finding same objects assigned to a string in an ArrayList

  2. 2

    Finding Recurring String in a TEXT file using PHP

  3. 3

    get list of unique objects from an arraylist in java

  4. 4

    Map of Objects, can't access ArrayList element by index

  5. 5

    Objects to ArrayList

  6. 6

    GeoDjango: Finding objects in radius

  7. 7

    Finding Index of in combination with RegEx

  8. 8

    Java ArrayList.remove(index)の問題

  9. 9

    Jackson: Convert JSON to object: ArrayList of objects with arraylist of objects with arraylist of objects

  10. 10

    java.lang.IndexOutOfBoundsException:Index:1、Size:0 at java.util.ArrayList.get(ArrayList.java:437)

  11. 11

    Finding the index of a sublist in a Numpy Array

  12. 12

    Finding the cell to the right of the index match

  13. 13

    Finding index out of bounds exception

  14. 14

    ArrayListのJava ArrayList

  15. 15

    Java JDT finding methods

  16. 16

    JAVA: How do I merge objects from an arrayList based on a property value?

  17. 17

    finding the force needed based on distances between objects

  18. 18

    Finding maximum and minimum Inside array of objects

  19. 19

    finding index of key in an ordered dictionary in powershell

  20. 20

    Finding the index of a date in a DateTime object python

  21. 21

    Finding the index of occurrence before the last occurrence of a string

  22. 22

    Finding the index or unique values from a dataframe column

  23. 23

    Finding index of sublist in list python 3

  24. 24

    Finding the number at a specific index of spirally filled matrix

  25. 25

    Finding the column index for the first numeric or date column

  26. 26

    JavaのArrayListのArrayList

  27. 27

    Finding specific range of number in java

  28. 28

    Out-projected type 'ArrayList<*>' prohibits the use of 'public open fun add(index: Int, element: E): Unit defined in java.util.ArrayList'

  29. 29

    Create index in Mongoose for array of objects

ホットタグ

アーカイブ