/** * 获取 list 是否为空(true) * @return list 是否为空 */ boolean isEmpty();数组方法实现代码
时间: 2024-02-26 08:50:07 浏览: 33
Sure! Here is an example implementation of the `isEmpty()` method using an array:
```java
public class MyList {
private int[] array;
public MyList() {
array = new int[0];
}
public boolean isEmpty() {
return array.length == 0;
}
}
```
In this example, the `isEmpty()` method checks if the length of the array is 0, indicating that the list is empty. If the length is 0, it returns `true`; otherwise, it returns `false`.
相关问题
Java工具类(四) 判断数组/集合为空的工具类
以下是一个Java工具类,可以用于判断数组、List、Set等集合类型是否为空:
```java
public class CollectionUtils {
/**
* 判断数组是否为空
* @param array 数组对象
* @return true:数组为空,false:数组不为空
*/
public static boolean isEmpty(Object[] array) {
return array == null || array.length == 0;
}
/**
* 判断List集合是否为空
* @param list List集合对象
* @return true:List集合为空,false:List集合不为空
*/
public static boolean isEmpty(List<?> list) {
return list == null || list.isEmpty();
}
/**
* 判断Set集合是否为空
* @param set Set集合对象
* @return true:Set集合为空,false:Set集合不为空
*/
public static boolean isEmpty(Set<?> set) {
return set == null || set.isEmpty();
}
/**
* 判断Map集合是否为空
* @param map Map集合对象
* @return true:Map集合为空,false:Map集合不为空
*/
public static boolean isEmpty(Map<?, ?> map) {
return map == null || map.isEmpty();
}
}
```
使用方法:
```java
String[] arr = new String[]{"a", "b", "c"};
List<String> list = new ArrayList<>();
Set<String> set = new HashSet<>();
Map<String, String> map = new HashMap<>();
System.out.println(CollectionUtils.isEmpty(arr)); // false
System.out.println(CollectionUtils.isEmpty(list)); // true
System.out.println(CollectionUtils.isEmpty(set)); // true
System.out.println(CollectionUtils.isEmpty(map)); // true
```
用java写一个判断多种类型的参数是否为空的工具类
可以写一个 `ParamUtil` 工具类来判断多种类型的参数是否为空,具体实现如下:
```java
public class ParamUtil {
/**
* 判断字符串是否为空
*
* @param str 待判断的字符串
* @return 是否为空
*/
public static boolean isEmpty(String str) {
return str == null || str.trim().length() == 0;
}
/**
* 判断集合是否为空
*
* @param collection 待判断的集合
* @return 是否为空
*/
public static boolean isEmpty(Collection<?> collection) {
return collection == null || collection.isEmpty();
}
/**
* 判断数组是否为空
*
* @param array 待判断的数组
* @return 是否为空
*/
public static boolean isEmpty(Object[] array) {
return array == null || array.length == 0;
}
/**
* 判断Map是否为空
*
* @param map 待判断的Map
* @return 是否为空
*/
public static boolean isEmpty(Map<?, ?> map) {
return map == null || map.isEmpty();
}
/**
* 判断对象是否为空
*
* @param obj 待判断的对象
* @return 是否为空
*/
public static boolean isEmpty(Object obj) {
if (obj == null) {
return true;
}
if (obj instanceof String) {
return isEmpty((String) obj);
}
if (obj instanceof Collection) {
return isEmpty((Collection<?>) obj);
}
if (obj instanceof Object[]) {
return isEmpty((Object[]) obj);
}
if (obj instanceof Map) {
return isEmpty((Map<?, ?>) obj);
}
return false;
}
}
```
使用时可以直接调用 `ParamUtil.isEmpty()` 方法,例如:
```java
String str = null;
if (ParamUtil.isEmpty(str)) {
System.out.println("字符串为空");
}
List<String> list = new ArrayList<>();
if (ParamUtil.isEmpty(list)) {
System.out.println("集合为空");
}
String[] array = new String[0];
if (ParamUtil.isEmpty(array)) {
System.out.println("数组为空");
}
Map<String, Object> map = new HashMap<>();
if (ParamUtil.isEmpty(map)) {
System.out.println("Map为空");
}
Object obj = null;
if (ParamUtil.isEmpty(obj)) {
System.out.println("对象为空");
}
```