2

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? Commented Oct 9, 2017 at 14:27
  • what is the error Commented Oct 9, 2017 at 14:27
  • Nouman Ch " It cannot find symbol class Kwas " but when i make it not in array everything works. Commented 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? Commented Oct 9, 2017 at 14:52

5 Answers 5

6

You can do this:

List<Kwas> list = new ArrayList<Kwas>();
list.add(a2);
Sign up to request clarification or add additional context in comments.

3 Comments

Also you can try list.add(new Kwas("Kwas Siarkowodorowy", "H2S"));
list.add(a2); In error i have got there "<identifier> expected"
Make sure that you have import java.util.ArrayList; import java.util.List;
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 Comments

But in android studio it cannot find symbol add.
Make sure that the array is type of ArrayList.
@JakubLelek just accept this answer it solved your problem. Thanks
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 Comments

The question is about android studio not plain java, android studio don´t support that kind of array
I don't know what you're talking about. Android Studio absolutely supports arrays of objects.
1

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

Then .add(object) on every object

Comments

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;

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.