- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Preconditions - Java
Precondition to check if the list passed as parameter is empty or not. Let us see an example −
Example
public void my_fun(List<Object> myList){ if (myList == null){ throw new IllegalArgumentException("List is null"); } if (myList.isEmpty()){ throw new IllegalArgumentException("List is empty"); } my_fun(myList); }
A void function named ‘my_fun’ is defined that takes a list of objects as its parameters. If the list is null, it prints the relevant message. If the list has no elements in it, a specific message is displayed. The function is called by passing the list as a parameter. This code snippet is a pre-condition which can be used to check if the list is empty or null.
Advertisements