1
Object[] Flights = new Object[10];

This is an Object array, now what i want is to add 4 attributes to each object which includes String and integer?

How can I do this ?

3
  • You need to create your own custom class that has the 4 attributes you want and then make an array of that rather than an array of Object. Commented Apr 26, 2020 at 1:05
  • You should start with oracle.com/technetwork/java/prog-140388.html Commented Apr 26, 2020 at 1:26
  • If you're using Java 14, you can use a record, which is a simple class that just contains data. Commented Apr 26, 2020 at 1:42

2 Answers 2

0

You need to create a custom class and define the attribute that you want

public class Solution {

  static class CustomObj {
     int id;
     String name;
     // your properties
  }

  public static void main(String[] args) {
       CustomObj[] objsC = new CustomObj[10];
       // your logic
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I believe you can't do this in Java, you absolutely need to create a class.

 public class Myclass {
      public String myString;
      public int myInteger;

 }

and create an instance of that class :

public mainClass {
   public void main(String args[]){

    MyClass myInstance = new MyClass();
    myInstance.myString = "the value you want";
    myInstance.myInteger = 1;
 }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.