How can I store an array of unknown, mixed primitives that are read from a schema file at runtime (Java)?

StackOverflow https://stackoverflow.com/questions/21612846

  •  08-10-2022
  •  | 
  •  

Question

I'm reading in a schema file by line as strings to determine the format of a separate binary file.

How can I convert/store the schema (represented as a string) to an array of mixed primitive data types to allow binary processing that would avoid the overhead of constant string comparisons for each element in the binary file?

Example: int int double char char int

Desired array or ArrayList: typeInt, typeInt, typeDouble, typeChar...

Gratzie

Was it helpful?

Solution

Use an Object array:

Object[] objArr = new Object[10];
objArr[0] = 1;
objArr[1] = "lol";
System.out.println(objArr[0]);
System.out.println(objArr[1]);

This should give:

1
lol

OTHER TIPS

According to me, first you have to use the Wrapper Classes in java to Parse the String to the desirable type and then store these types to ArrayList because arraylist has good memory management as well as many ready mate methods, so it is very easy to work with it

You can take some hint from this peace of code

int foo = Integer.parseInt("1234");
String xyz = "hello";

List list1 = new ArrayList<>();
        list1.add(foo);
        list1.add(xyz );

You can also create a ArrayList of Objects.

import java.util.ArrayList;

ArrayList<Object> listObj = new ArrayList<Object>();
listObj.add(1);
listObj.add("Some string");
listObj.add(new ArrayList<Object>());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top