how to set ArrayList [duplicate] - java

This question already has answers here:
The local variable n may not have been initialized
(4 answers)
Closed 4 years ago.
I'm still newbie and still learn Java for Android. I have problem when call setIsiarray that always crash.
public class sebuaharray {
ArrayList<String> isiarray;
ArrayList<String> lab;
public ArrayList<String> getIsiarray() {
return isiarray;
}
public void setIsiarray(ArrayList<String> isiarray) {
this.isiarray = isiarray;
}
}
When I call setIsiarray such as this function
public void isiarraynya(){
ArrayList<String> arraysementara=new ArrayList<>();
arraysementara.add("Coba nambah");
sebuaharray arraynya;
arraynya.setIsiarray(arraysementara);}
Is there any solutions for my problem? Sorry for my bad english :-(

You are calling the setIsiarray method on a variable that is not initialized.
sebuaharray arraynya;
What you need is to instantiate an object using new keyword
sebuaharray arraynya = new sebuaharray();
arraynya.setIsiarray(arraysementara);}

You must create an object of class correctly
sebuaharray arraynya = new sebuaharray();
Next in sebuaharray class you must define the array list properly before assigning it
ArrayList<String> isiarray = new ArrayList<String>();
ArrayList<String> lab = new ArrayList<String>();

Related

Local variables referenced from an inner class must be final or effectively final - Java [duplicate]

This question already has answers here:
Why are only final variables accessible in anonymous class?
(15 answers)
Closed yesterday.
When attempting to initialize an ArrayList with an index as a value, I encounter the error message "local variables referenced from an inner class must be final or effectively final at <add(index);>"
int index=0;
for (int i:nums){
if (!map.containsKey(i)){
ArrayList<Integer> al1=new ArrayList<Integer>(){{
add(index);
}};
map.put(i,al1);
}
index+=1;
}
I know there are possible walkarounds where I can just simply declare arraylist then add value to it separately, this works totally fine.
ArrayList<Integer> al1=new ArrayList<Integer>();
al1.add(index);
map.put(i,al1);
But I want to understand whether there's any way to achieve it during initialization itself.
Please help me with this. Thanks in advance!
You can initialize a temporary variable to the current index value and use that.
final int temp = index; // this is effectively final
ArrayList<Integer> al1=new ArrayList<Integer>(){{
add(temp);
}};
However, in this case, it is probably better to use List.of, Arrays.asList, or Collections.singletonList.
List<Integer> al1 = List.of(index);
// or, if an ArrayList is specifically required:
ArrayList<Integer> al1 = new ArrayList<>(List.of(index));

Creating instance of an object with String[] as one of the parameters [duplicate]

This question already has answers here:
How do I declare and initialize an array in Java?
(31 answers)
Closed 4 years ago.
I was wondering how to create an instance of an object that takes a String[] type as a parameter. So far it comes up with an error saying that I cannot initialise an array when creating a new instance. This is my code:
public class Profile{
public Profile(String[] interests) {
this.interests = interests;
}
}
public class ProfileTest{
public static void main(String[] args) {
//Where the error appears
Profile newProfile = new Profile({"Interest1","Interest2","Interest3"});
}
}
I don't want to use an arraylist or anything - just String[].
To fix the compilation error, use a variable to hold the string array like this:
String[] array = {"Interest1","Interest2","Interest3"};
Profile newProfile = new Profile(array);

static variable declaration in class java [duplicate]

This question already has answers here:
Java: When is a static initialization block useful?
(13 answers)
Closed 5 years ago.
Why is this declaration wrong? This declaration leads to identifier expected error
class Abc{
static ArrayList<Integer> p;
p = new ArrayList<Integer>(); // identifier expected error
}
You have a freestanding assignment statement in your class body. You can't have step-by-step code there, it has to be within something (an initializer block, a method, a constructor, ...). In your specific case, you can:
Put that on the declaration as an initializer
static ArrayList<Integer> p = new ArrayList<>();
Wrap it in a static initialization block
static {
p = new ArrayList<Integer>();
}
More in the tutorial on initializing fields.
This is the right way to do it :
import java.util.ArrayList;
public class Abc {
static ArrayList<Integer> p;
static {
p = new ArrayList<Integer>(); // works
}
}

Java - Create ArrayList from type passed as a parameters [duplicate]

This question already has answers here:
Using and declaring generic List<T>
(3 answers)
Closed 6 years ago.
Im looking to see if it is at all possible to create an array of type, where the type is passed in as a parameters for example:
public void doSomething(Class<?> itemClass){
ArrayList<itemClass> newItems = new ArrayList<itemClass>();
}
But that obviously doesn't compile. Is it possible to do something like this?
Try this:
private <T> void makeTypedArray(Class T) {
ArrayList<T> typedArray = new ArrayList();
}
The above is the only way I know of to pass a usable type to a method in Java.

Why array instantiation with a generic type does not work in Java? [duplicate]

This question already has answers here:
What's the reason I can't create generic array types in Java?
(16 answers)
Closed 8 years ago.
public interface Foo<E> {
public void blah(E e);
}
public class MyClass<E> {
private final Foo<E>[] fooArray = new Foo<E>[8]; // does not compile!
// (...)
}
What is the correct way of addressing that generics limitation?
Or maybe it is not a limitation and I am missing something?
The make-it-compile way would be declaring the variable using generics but initializing the array (not the elements) using raw type:
//add #SuppressWarnings("unchecked") to avoid warnings
Foo<String>[] arrayOfFoo = new Foo[8];
arrayOfFoo[0] = new ClassThatImplementsFoo<String>();
//line below gives a compile error since the array declaration is for Foo<String>
//and won't allow Foo<AnotherClass>
arrayOfFoo[1] = new ClassThatImplementsFoo<Integer>();
A better and safer approach would be using List instead of plain array:
List<Foo<String>> fooList = new ArrayList<Foo<String>>();
fooList.add(new ClassThatImplementsFoo<String>());
In your code, you should do this:
public class MyClass<E> {
private final Foo<E>[] fooArray = new Foo[8];
// (...)
}

Categories