2

Say we have an array named L and an input string in.
If in has a form like this [0,1,2,3], L should be an int[] containing the numbers 0, 1, 2 and 3, but if in is like this <0,1,2>, then L should be a String[] containing the Strings "0", "1" and "2".
Is this possible to do in Java? Or is there a way to define an array that accepts whatever type you put into it and then behaves like an array of that type?

I've thought about why I need this and came to the conclusion that I can kind of work around it. It requires some manual writing in the actual code, which means that it'll never be a compilable program, but unless a solution is found, I'll just use this dirty solution. Thank you for your answers!

EDIT: It seems like this problem is impossible to solve the way I want it with natural Java. I'll use the dirty solution as described above. Once again, thank you anyways. Also that strange edit was me, I didn't realize I wasn't logged in.

3
  • 2
    The closest thing I can suggest is an Object[]. Commented Jan 28, 2018 at 17:06
  • It's actually Object, since an int[] is not an Object[]. Unless you store Integers, and not ints. Commented Jan 28, 2018 at 17:11
  • 3
    Why do you need / want to do this? What is the XY problem? Commented Jan 28, 2018 at 17:13

3 Answers 3

1

As Joe C suggested, you can store it as an Object[].

And when you need to utilize it, you can cast the whole Object[] into a int[] or String[] using code like this:

Integer[] integerArray = Arrays.copyOf(a, a.length, Integer[].class);
int[] intArray = Arrays.stream(integerArray).mapToInt(Integer::intValue).toArray();

or

String[] stringArray = Arrays.copyOf(a, a.length, String[].class);
Sign up to request clarification or add additional context in comments.

2 Comments

If the underlying array the Object[] variable is a reference to is actually of type String[] or Integer[] (e.g. Object[] a = new String[5]), you can (and should) just cast it instead of using copyOf.
After reading it again, the user's getting this in the form of a in string. Which like you mentioned, begs the XY question. If it's coming in as a String in the first place, why would you need to make it an Object at the start.
1

You can use an Object array because Integer and String are Object:

           java.lang.Object
                  |
       +----------+----------+  
       |                     |
       v                     |
java.lang.Number             |
       |                     |
       v                     v
java.lang.Integer    java.lang.String

Exemple:

Object[] array = new Object[] {
    0,          //integer value
    "hello",    //String value
}

or

Object[] array = new Object[2];
array[0] = 0;          //integer value
array[1] = "hello";    //String value

2 Comments

Object[] L = {0,1}; System.out.println(L[1]-L[0]); This won't work, but I need it to work.
To use the array elements as integers, you have to cast them to integers: Object[] L = {0,1}; System.out.println((int) L[1] - (int) L[0]);
0

If there are always two type of String, then

String s = "<1,2,4>";
Object[] oArr;
if (s.contains("["))
{  
    //extract the string between the bracket
    s = s.substring(s.indexOf("[") + 1);
    s = s.substring(0, s.indexOf("]"));
    // split by comma, which return String array and convert to Integer[]
    oArr = Arrays.stream(s.split(",")).mapToInt(Integer::parseInt).boxed().toArray(Integer[]::new);

}
else
{ 
    //extract the string between the bracket
    s = s.substring(s.indexOf("<") + 1);
    s = s.substring(0, s.indexOf(">"));
    // split by comma, which return String array  
    oArr = s.split(",");
}

3 Comments

This produces either an array called iArr or one called sArr. I need both var names to be the the same.
@mindoverflow JBNizet already mentioned that's not possible until we store them into Object[]. If you are thinking about object array then, it can't be int[] instead Integer[]. Are you fine with then ?
@mindoverflow I have updated my posted as you asked.

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.