Win a copy of Kotlin Cookbook this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Arrays.fill myClass[]

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to fill an array with "-" values and own myClass[]? With Object[] it is working, not with myClass[].

 
Saloon Keeper
Posts: 11020
243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because "-" is a string, and strings are also objects. Strings are not instances of myClass.

What do you hope to achieve by filling an array of your custom type with instances of string?
 
Alexander Visser
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Array must be initialized with [-, -, -, -, -, -, -, -, -, -]
Then a few myClass objects will be added like [-, -, -, myClass, -, -, -, -, -, myClass]
That's why I need to fill the array with - first.
 
Marshal
Posts: 67050
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alexander Visser wrote:Array must be initialized with [-, -, -, -, -, -, -, -, -, -]
Then a few myClass objects will be added like [-, -, -, myClass, -, -, -, -, -, myClass] . . . .

The two requirements are mutually incompatible. The only way you can get that code to compile is by giving your array a runtime type of Object[].
 
Stephan van Hulst
Saloon Keeper
Posts: 11020
243
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general it's a bad idea to mix types like this.

What is the ultimate purpose of your application?
 
Campbell Ritchie
Marshal
Posts: 67050
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

About 3o'clock, I wrote:. . . . The only way you can get that code to compile . . .

I have tried the code, which does compile, but throws an exception at runtime. That is because Arrays#fill() takes Object[] rather than T[] as its first parameter.
 
Saloon Keeper
Posts: 6619
62
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this actually a display issue. In other words, if an array element is null then print "-" else print element.toString().
 
Campbell Ritchie
Marshal
Posts: 67050
255
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good suggestion That would solve the problem, but you would have to write your own print method. You can't do that by extending myClass[] but you can write a wrapper.

By the way: It should read MyClass not myClass.
 
Alexander Visser
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed a good suggestion. Thank you very much.

 
Campbell Ritchie
Marshal
Posts: 67050
255
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or I might prefer to condense lines&#a0;7‑13 intoYou don't have to write toString() explicitly. Or you can use a StringJoiner object which allows you to specify separators and start and end characters.
 
Do not threaten THIS beaver! Not even with this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!