1

I have a long piece of code that looks like this

Kwas a1 = new Kwas("Kwas Azotowy(V)", "HNO3");
// etc..
Kwas a17 = new Kwas("Kwas FluorkoWodorowy", "HF");

How can I write it as an Array? I tried something like

Kwas[] a = new Kwas[17] 

But it didn`t work.

My "Kwas" class looks like the following:

public class Kwas {
String name;
String formula;

public Kwas( String nazwa, String wzor)
{
    name = nazwa;
    formula = wzor;
}

void setName(String c)
{
name = c;
}
void setFormula(String c)
{
    formula = c;
}
public String getName()
{
    return name;
}
public String  getFormula() {return formula;}

}

4
  • You said "it didn't work", but I am curious as to what error you encountered when you tried your code? Oct 9, 2017 at 14:27
  • what is the error
    – Nouman Ch
    Oct 9, 2017 at 14:27
  • Nouman Ch " It cannot find symbol class Kwas " but when i make it not in array everything works. Oct 9, 2017 at 14:33
  • 1
    is your Kwas class in the same package? if not are you importing it to your current class? Oct 9, 2017 at 14:52

5 Answers 5

5

You can do this:

List<Kwas> list = new ArrayList<Kwas>();
list.add(a2);
3
  • 1
    Also you can try list.add(new Kwas("Kwas Siarkowodorowy", "H2S")); Oct 9, 2017 at 14:31
  • list.add(a2); In error i have got there "<identifier> expected" Oct 9, 2017 at 14:48
  • Make sure that you have import java.util.ArrayList; import java.util.List; Oct 9, 2017 at 14:52
3

Just implement an ArrayList like this:

ArrayList<Kwas> newArray= new ArrayList<>();

And then:

newArray.add(a2);
newArray.add(a3);
newArray.add(a4);
newArray.add(a5);
newArray.add(a6);
newArray.add(a7);  
...
...

Then if you want to get an specific item just write something like this:

newArray.get(1).getName(); //for example
3
  • But in android studio it cannot find symbol add. Oct 9, 2017 at 14:42
  • Make sure that the array is type of ArrayList. Oct 9, 2017 at 14:44
  • @JakubLelek just accept this answer it solved your problem. Thanks Oct 9, 2017 at 15:36
1

I can't comment yet, so I have to provide it as an answer. Everyone is answering here how OP can construct a List, but no one is actually answering how he can create an array, which is probably very confusing for OP who might now think you can't create arrays of self-defined objects. You definitely can. But I don't know what the problem is.

Kwas[] a1 = new Kwas[17];

is definitely the right syntax. Are you sure you include the class? Can you post the exact code and error?

My guess is that you didn't import your class. In Android Studio, try placing your cursor after Kwas and pressing Ctrl+Space. This should show a dropdown list. Select the first line and press enter. Now it should have added an import to your class.

2
  • The question is about android studio not plain java, android studio don´t support that kind of array
    – Moshe Edri
    Oct 9, 2017 at 17:19
  • I don't know what you're talking about. Android Studio absolutely supports arrays of objects.
    – MrZarq
    Oct 9, 2017 at 21:08
1

ArrayList<yourObjectName> arrayName = new ArrayList<yourObjectName>();

Then .add(object) on every object

0

You can simply type:

ArrayList<ObjectType> arrayName = new ArrayList<ObjectType>();

Adding Elements:

arrayName.add(someObject);

Removing Elements:

arrayName.remove(arrayName.get(someInteger));

Getting Elements:

arrayName.get(someInteger);

PS: Don't forget to import:

import java.util.ArrayList;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.