Is there a way to use the toArray() method on an ArrayList<CustomObject>?
From what I see, it can only be used with Object
You need to pass in an array of CustomObject in order to get one back. The parameter-free ArrayList.toArray() call returns an Object[], but the parameterized version T[] ArrayList<T>.toArray(T[]) returns what you expect. If you size the array you pass as a parameter correctly then the call will use the array you pass rather than allocate another one, e.g.
ArrayList<CustomObject> foo;
//...
CustomObject[] bar = foo.toArray(new CustomObject[foo.size()]);
Use:
CustomObject[] customObjects = myList.toArray(new CustomObject[myList.size()])
CustomObj[] customArray = new CustomObj[size];
customArray = (CustomObj[])ArrayListObj.toArray(customArray);
Related
We can add a string to an ArrayList<String[]> as:
ArrayList<String[]> array1 = new ArrayList<>();
array1.add(new String[]{"word"});
But how can we add a string to an ArrayList<ArrayList<String[]>> directly without creating array1. Something like:
array2.add(new ArrayList<>(new String[]{"hello"}));
You could use
array2.add(new ArrayList<>(Arrays.<String[]>asList(new String[]{"hello"})));
There is no ArrayList(ArrayOfElements) constructor, but we can use ArrayList(CollectionOfElementsToCopy) constructor. With that all we need to do is wrap elements into some collection. For that we can use Arrays.asList(elements).
Problem with Arrays.asList is that it uses T... varargs which by default represents array of elements. If we want to tell that array is element we can do it by explicitly specifying <String[]> as method generic type.
If I have an observableList data1 = FXCollections.observableArrayList();
I want to stick it in an array because I have a bunch of them.
Can I do that ?
If yes, whats the syntax ?
List interface provides a method called toArray() to convert a list to array. This should do:
data1.toArray();
EDIT
As per the questioner input, you may need this:
observableList[] observableListArray = new observableList[SIZE_YOU_NEED];
observableListArray[index] = data1;
Suppose I have class FooClass.
public class FooClass {
}
The following line gives me the following compile error:
// Note I want to create an array of length 4 of Lists of FooClass
List<FooClass> runs[]=new List<FooClass>[4];
Cannot create a generic array of List<FooClass> ...
Would appreciate any help.
List collection is not the same as array:
// if you want create a List of FooClass (you can use any List implementation)
List<FooClass> runs = new ArrayList<FooClass>();
// if you want create array of FooClass
FooClass[] runs = new FooClass[4];
UPD:
If you want to create array of lists, you should:
Create array
Fill this array in with List instances
Example:
List<FooClass>[] runs = new List[4];
for (int i = 0; i < runs.length; i++) {
runs[i] = new ArrayList<>();
}
It's not good idea to mix Generics and Array. Generics doesn't retain type information at run time so creating an array of generics fails.
List should not be declared as array. It should be:
List<FooClass> runs=new ArrayList<FooClass>(4);
or
List<FooClass> runs=new ArrayList<FooClass>();
Edit: you can try List<ConfigParser> runs[] = new List[4];. But why do you need array of Lists?
Also as #rai.skumar has mentioned, Generic information is not retained during run time because of Type Erasure.
I want to define an array which its elements are object in java and i can use for ex this method:
public void add(Object elem);
Can anyone help me on this?
A literal object array is defined as any other array.
Object[] objects = new Object[10];
Though, more likely, you are looking for something like ArrayList
List<Object> object = new ArrayList<Object>();
Which will give you the .add functionality because it's a List.
Objecttype objects= new Objecttype [initial size]
Why you want to use arrays. go for ArrayList in which you don't bother to define your initial size also it provides its own add method
Defining an array of objects is just like defining any other array
Object[] objectList = new Object[MAX_LIMIT];
for(int i=0;i<objectList.length;i++){
objectList[i] = new ANY_DATA_TYPE();
}
I have the class:
class SomeClass<T extends SomeInterface>{
private T[] myArray;
public SomeClass()
{
// I want to initialize myArray in here to a default size of 100
myArray = new T[100]; // this gives an error
}
}
I know I can fix that by requiring a parameter in the constructor as:
class SomeClass<T extends SomeInterface>{
private T[] myArray;
public SomeClass(Class<T> clazz)
{
myArray= (T[]) Array.newInstance(clazz, 100);
}
}
but it makes no scene having to pass the generic parameter twice.
in other words in order to instantiate an object from the class SomeClass I will have to do something like:
SomeClass<SomeOtherClass> obj =
new SomeClass<SomeOtherClass>(SomeOtherClass.class);
I program in c# and Java does not seem to be friendly. I don't even understand why it is not possible to cast Object[] array to SomeOtherClass[] array. In c# that will be possible...
so my question is how can I avoid having to pass the SomeOtherClass.class parameter in order to be able to construct an array of the generic type in the constructor of the class...
While Shakedown listed the fix to your problem, allow me to explain why it is not typesafe to create a generic array.
I will illustrate why with an example from Effective Java 2nd Ed.
// Why generic array creation is illegal - won't compile!
List<String>[] stringLists = new List<String>[1]; // (1)
List<Integer> intList = Arrays.asList(42); // (2)
Object[] objects = stringLists; // (3)
objects[0] = intList; // (4)
String s = stringLists[0].get(0); // (5)
Let’s pretend that line 1, which creates a generic array, is legal.
Line 2 creates and initializes a List<Integer> containing a single
element.
Line 3 stores the List<String> array into an Object array
variable, which is legal because arrays are covariant.
Line 4 stores the List<Integer> into the sole element of the Object array, which
succeeds because generics are implemented by erasure: the runtime type
of a List<Integer> instance is simply List, and the runtime type of a
List<String>[] instance is List[], so this assignment doesn’t generate
an ArrayStoreException. Now we’re in trouble. We’ve stored a
List<Integer> instance into an array that is declared to hold only
List<String> instances.
In line 5, we retrieve the sole element from
the sole list in this array. The compiler automatically casts the
retrieved element to String, but it’s an Integer, so we get a
ClassCastException at runtime. In order to prevent this from
happening, line 1 (which creates a generic array) generates a
compile-time error.
Yes, you would have to pass in the .class like that in order to make this work.
You could avoid all of this and just use an ArrayList<T> instead. When you need it in the form of an array you can use: (T[]) myArrayList.toArray()
All and every type in java is a subclass of Object class so this can be achieved using this.
public StackArray(int size){
dataStack = (T[])new Object[size];
}