简体   繁体   中英

Retrieve value from int array inside objects ArrayList

i am experementing on ArrayList which contains different data types and an int array and I want to access a value from the array for example number 55. Can I directly access this array without using for-loops? How to override the toString method of this int array, so that I dont get entries like this: [I@2401f4c3? Also I dont know how to define for loop correcly because it prints the array just 1 time in console but it should print 4 times. Have any ideas please share with us.

ArrayTest class:

public class ArrayTest {

    public static void main(String[] args) {
        ArrayList<Object> strangeList = new ArrayList<>();

        strangeList.add("String");
        strangeList.add(3823);
        strangeList.add(new int[] { 329, 23882, 55, 932 });

        System.out.println(strangeList.size());
        System.out.println();

        for (int i = 0; i < strangeList.size(); i++) {
            if (i == 2) {
                for (int j = 0; j < strangeList.size(); j++) {
                    System.out.println(strangeList.get(j));
                }
            }
        }
    }
}

Using obj.getClass().isArray() , check the type of obj if it is an array and if yes, print Arrays.toString((int[]) obj) ; otherwise, print obj where obj is an element from strangeList .

Demo:

import java.util.ArrayList;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        ArrayList<Object> strangeList = new ArrayList<>();

        strangeList.add("String");
        strangeList.add(3823);
        strangeList.add(new int[] { 329, 23882, 55, 932 });

        System.out.println(strangeList.size());
        System.out.println();

        for (int i = 0; i < strangeList.size(); i++) {
            Object obj = strangeList.get(i);
            Class<?> claz = obj.getClass();
            if (claz.isArray()) {
                if (claz.getComponentType() == int.class) {
                    System.out.println(Arrays.toString((int[]) obj));
                } else if (claz.getComponentType() == double.class) {
                    System.out.println(Arrays.toString((double[]) obj));
                }
                // ...
            } else {
                System.out.println(obj);
            }
        }
    }
}

Output:

3

String
3823
[329, 23882, 55, 932]

The second for loop you have goes over the same array not your int array to go through your int array you need to get that first. But since the starting array is of type object you need to "tell" (cast) java that it's actually an int array.

for (int i = 0; i < strangeList.size(); i++) {
    if (i == 2) {

        int[] myIntArray = (int[]) strangeList.get(2);
        for (int j = 0; j < myIntArray.length; j++) {
            System.out.println(myIntArray[j]);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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