Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

check an array if is empty or if it has something on it  RSS feed

 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how could i do that?
 
Stephan van Hulst
Saloon Keeper
Posts: 7806
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean checking if the array has no elements, or if all elements are null?

In the first case, just check whether the length of the array is 0.

In the second case, you can use a combination of Arrays.stream(), Stream.allMatch() and Objects.isNull().
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Do you mean checking if the array has no elements, or if all elements are null?

In the first case, just check whether the length of the array is 0.

In the second case, you can use a combination of Arrays.stream(), Stream.allMatch() and Objects.isNull().

could you help me with a code?to understand better?
 
Liutauras Vilda
Marshal
Posts: 4638
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us what have you tried so far after Stephan gave you couple of hints.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ekte spiriopoulos wrote:could you help me with a code?to understand better?

Ekte, you really need to try some things for yourself. We're happy to help, but we like you to ShowSomeEffort first.

1. We're still not sure exactly what you want.
2. How would YOU do it?

Try writing some code, and come back if you run into any problems.

I also suggest you have a look at our HowToAskQuestionsOnJavaRanch page.

Winston
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is this good?
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you need to define what you mean by empty array.

Your code is not good. Whatever the length of the array is, this code will do nothing.
If the length is 0, the body of the for loop will not be executed.
If the length is greater than 0, the body of the loop will be executed but the if condition will not do anything.

By the way, you don't need a loop to check the length of the array. Just use the if condition.
 
Liutauras Vilda
Marshal
Posts: 4638
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ekte spiriopoulos wrote:
Liutauras Vilda wrote:Please show us what have you tried so far after Stephan gave you couple of hints.
i will try to something that isnt in my knowledges ?
Yes, this is what I meant. I did Google search by your subject name. Here are my findings. Then I clicked on the very top link and there is an answer in the second post from top.

When you writing programs, it is common even for very experienced programmers to look up for some information on internet, that is the main tool.

As an advice, I'd suggest you to keep in your browser always opened 2 pages (these are clickable links):
1. Oracle Tutorial
2. Java API

For the time being, you should be able to find all the answers to your questions for the next few years or so. The advantage of looking information individually is, that you discover many interesting things, so you get more and more questions - so you learning

And what Winston mentioned, you need to try that out yourself first. Apart from that, everyone here in the Ranch are more than happy to help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!