1

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));
                }
            }
        }
    }
}

2 Answers 2

3

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]
Sign up to request clarification or add additional context in comments.

Comments

1

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]);
        }
    }
}

3 Comments

Hello Teh, thank you for your comment. I already try this out but The method size() is undefined for the type Object. It is not possible :/
Hey this one works fine thank you! I am thinking of making it more dynamically like what if it was an double[] instead of int[]. How we can check which type of array we have?
With something like this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.