This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Array Length/Size

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am doing a simple (but not for me) JDBC implementation with java. I have this customer class which puts customers into an array:



I am trying to create a new customer in the event there is no customer match. I need to create a new ID. I think the best way to do this is to reference the length of the array and +1 but I am not sure how to do that. I have the following code:



I think I MIGHT be on the right track with:



But I don't feel null is the right value and I don't know what value should go there.


 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally we let the DB determine the IDs for us, such as with a sequence.

If you're going to do it yourself, then taking the highest ID present and adding 1 is a valid approach, but it can be a hassle to keep track of it in all situations, especially if your app is multithreaded. You're really better off letting the DB manage it.
 
Meg Berg
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just an assignment for a database class and so I realize the implementation may not be a real world scenario.

How do I create the ID within the confines of needing to do it in the java code? Is (null) the right value on the .toArray?

Thanks!

Meg
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Meg Berg wrote:This is just an assignment for a database class and so I realize the implementation may not be a real world scenario.

How do I create the ID within the confines of needing to do it in the java code? Is (null) the right value on the .toArray?

Thanks!

Meg


it's not right, why use null?

the parameter means:
the array into which the elements of this collection are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.

if the specified array is null, there would be NullPointerException.
 
Meg Berg
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if I seem dense, it isn't intentional, I assure you. In my OP, I agreed that I didn't think null was the right value, but I am totally lost as to what I SHOULD put in there or if that will even help me do what I need it to do.

Meg
 
Marshal
Posts: 67273
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to call toArray() you already need a collection of customer instances. And if you have that, you already know the count and don't need the array at all.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!