-8

I need to create an arraylist, that contains an array of objects to act as the rows of a table, like this:

ArrayList<Object[]> rows = new ArrayList<>();

each of these Object[] arrays will contain the values of the attributes of my Publisher class, like this example:

["123", "McGraw hill", "abc street uncanny valley"]

I know I can do this with either Object.values(p) or Object.keys(p). Since I have all my publishers in an arraylist I tried looping through it to fill the rows ArrayList:

for(Publisher p : publishers){ rows.add(Object.values(p)); } 

but the ide (Netbeans) says it cannot find the values or key method for the publisher class. The exact error message says:

cannot find symbol symbol: method values(Publisher) location class Object 

Why can't I use .value() ? Is there another way I can do this? All help is appreciated. Thanks!

7
  • 6
    Netbeans is correct; keys() and values() are not methods of the Object class. Can you please post a minimal reproducible example? Commented Nov 22 at 23:34
  • 9
    I think you may have mixed up Java with JavaScript. Object.keys and Object.values are functions in JavaScript but not Java. Despite the similar names, these are different programming languages. Commented Nov 23 at 0:54
  • 5
    "I know I can do this with either Object.values(p) or Object.keys(p)" -- no, that cannot be done with Java - The available methods can de found in the Javadoc of Object Commented Nov 23 at 0:57
  • 1
    Yes, you definitely confused Java and Javascript. If you're looking at MDN for Java, you're on the wrong site. Commented Nov 23 at 1:10
  • 1
    Hello Luna, welcome, this may be useful to you: for( Publisher p : publishers ) { rows.add( new Object[] { p.getA(), p.getB(), p.getC() } ); }, replacing getA() and similar methods with the getters of the Publisher class. Commented Nov 23 at 7:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.