I want to create a class (Array) that performs different methods on arrays. What I have so far is overloaded constructors for different types of arrays (ints, strings, etc). However this class will also need to create an array of classes, so how could I pass in a class name and have my Array class create an array of that class?
I could just hard code it in for the class I know I will make an array of but I want to make my Array class versatile enough to have this work with any class I make in the future.
As @andresp said, you can make a new class with an array as a private field. Another option to avoid having to pass a
Class<T>
as an argument in addition to the type parameter is to replace the array withjava.util.ArrayList
, which functions just like an array with a few differences.For example:
You can do something like:
which you can call instantiate by using:
I would also suggest a different name for your class as
Array
is already used by the Java API.