Question about declaration and initialization [duplicate] - java

This question already has answers here:
Difference between List and Array
(1 answer)
How to initialize an array in Java?
(11 answers)
Closed 3 years ago.
I have a question about initialization. When we initialize an array with { }, we must do it right after declaration to show compiler which type to use. Why does compiler allow diamond operator do it with 2 statements?
Integer[] array = {2,4,5};
//Integer[] array; array = {2,4,5}; - error
List<Integer> list = new ArrayList<>();
//List<Integer> list; list = new ArrayList<>(); - no error

You can do the two line way with arrays too, you just need to create a new object (and of course explicitly specify the type) when initializing.
Integer[] array;
array = new Integer[]{1, 2, 3};

Related

Object[] arr = {1,2,[3,4,[5]],6}; is this is possible [duplicate]

This question already has answers here:
Java Array with multiple data types
(3 answers)
How to declare an array of different data types [closed]
(6 answers)
Multiple types on a Java multi-dimensional array
(2 answers)
Closed 3 years ago.
I've been asked this question twice and i got confused either the question is correct or not. i searched and found nothing.
Object[] arr = {1,2,[3,4,[5]],6};
The question was to add all the objects of the above mentioned array.
No. That is not a valid syntax in Java.
You could do this though:
Object[] arr = new Object[] {
new Integer(1),
new Integer(2),
new Object[] {
new Integer(3),
new Integer(4),
new Object[] {
new Integer(5)
}
},
new Integer(6)
};
In python you can do:
arr = [1, 2, [3,4,[5]],6];
In JavaScript:
let arr = [1, 2, [3,4,[5]],6];

Java - Initialize array in constructor [duplicate]

This question already has answers here:
Syntax for creating a two-dimensional array in Java
(13 answers)
How do I declare and initialize an array in Java?
(31 answers)
Closed 5 years ago.
I want to initialize two dimensional array in constructor. But, I have a problem when I declare instance variable of array in class. It will be error if I make it like this:
public class Data {
private String [][] tabel;
public Data(){
tabel = {{"ID", "NAME"},
{"101", "Max"},
{"102", "Mark"},
{"103", "Downey"},
{"104", "Matthew"},
{"105", "Richard"}};
}
How I can solve this problem?
You need to write new Type[] in front of the array initializers like so:
tabel = new String[][]{
new String[]{"ID", "NAME"},
new String[]{"101", "Max"},
new String[]{"102", "Mark"},
new String[]{"103", "Downey"},
new String[]{"104", "Matthew"},
new String[]{"105", "Richard"}};

How to create an array of Object of some type? [duplicate]

This question already has answers here:
Initialize Java Generic Array of Type Generic
(2 answers)
Closed 7 years ago.
What's syntax for creating an array of Objects of some class type?
Object<SomeClassType<T>>[] array?
Creating an array of a generic type is not allowed, but what you can do, is creating one with wildcard and casting it. The cast will give you a warning, but as the array only contains nulls by default, it can be done safely.
#SuppressWarnings("unchecked")
Object<SomeClassType<T>>[] array = (Object<SomeClassType<T>>[]) new Object<?>[length]
A number of ways to declare an array of objects of some class type. For example,
1. MyClass[] myArray = new MyClass[# of elements];
Then initialize each as, myArray[0] = new MyClass();, myArray[1] = new MyClass(), etc..
or via a for-loop
2. MyClass[] myArray = {new MyClass(), new MyClass(), ...};
I would say something like this would allow you to create an array.
String[] testArray = new String[3];
testArray[0] = "one";
testArray[1] = "two";
testArray[2] = "three";
String[] testArray = {"one","two","three"};

Generic array creation error on ArrayList [duplicate]

This question already has answers here:
Generic array creation error
(5 answers)
How to create a generic array in Java?
(32 answers)
Closed 7 years ago.
I get the following error in my IDE "generic array creation"
I googled it but found very long explanations and didn't quite understand what the best solution is to this problem.
If anyone could suggest the best solution to this to get my code to compile...
public ArrayList<String>[] getClosedTicketIDs(Account account) {
ArrayList<String> closedSourceTickets = new ArrayList<>();
ArrayList<String> closedAccountTickets = new ArrayList<>();
// ...some unimportant to this example code...
// return
ArrayList<String>[] a = new ArrayList<String>[2]; // <-- generic array creation error
a[0] = closedSourceTickets;
a[1] = closedAccountTickets;
return a;
}
My objective is to return an array consisting of 2 ArrayList<String> (no more, no less).
You can only create raw array types. You need to do this: a = new ArrayList[2];
You cant do that but you can do
List<List<String>> a=new ArrayList<ArrayList<String>>();
but the better would be
ArrayList[] a=new ArrayList[n];
as you can fix the size in this.

Java:: Converting ArrayList Primitive into Array of Number class? [duplicate]

This question already has answers here:
make arrayList.toArray() return more specific types
(6 answers)
Closed 7 years ago.
If let say i have;
ArrayList <Double> myV = new ArrayList <Double>();
myV.add(12.2);
myV.add(3.2);
myV.add(5.00);
// this is error
Number[] youV = myV.toArray();
the below code is error when I compiled it. What should I do then to convert the ArrayList into Number of arrays type?
How to convert them into Number[] ?
And lastly, is this code list safe for us to use, if I apply this code
inside Android?
This code should do what you need.
Number[] result = new Number[myV.size()];
myV.toArray(result);
To answer your first question, You can convert them like this.
public static void main(String[] args) {
ArrayList<Double> myV = new ArrayList<Double>();
myV.add(12.2);
myV.add(3.2);
myV.add(5.00);
Number[] target = new Number[myV.size()];
myV.toArray(target);
System.out.println(target[0]);
}

Categories

Resources