/** * 获取 list 是否为空(true) * @return list 是否为空 */ boolean isEmpty();数组方法实现代码
时间: 2024-02-26 15:50:07 浏览: 97
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`.
相关问题
有一个数据库(rouble_personnel_pool),实体类为RoublePersonnelPool的字段参数类型有numbleId(序号),roubleUnit(隶属单位),roubleBumen(部门),roubleName(人员名称),roubleID(编号),而且都是string类型。以下是model层 @Data @Entity @Table(name = "rouble_personnel_pool") @EqualsAndHashCode(callSuper = true) public class RoublePersonnelPool extends BaseEntity { private static final long serialVersionUID = 1L; /** * 序号 */ @Column(name = "number_id") @ApiModelProperty("序号") @NotBlank(message = "序号不能为空") private String numberId; /** * 隶属单位 */ @Column(name = "rouble_unit") @ApiModelProperty("隶属单位") @NotBlank(message = "隶属单位不能为空") private String roubleUnit; /** * 部门 */ @Column(name = "rouble_bumen") @ApiModelProperty("部门") @NotBlank(message = "部门不能为空") private String roubleBumen; /** * 人员名称 */ @Column(name = "rouble_name") @ApiModelProperty("人员名称") @NotBlank(message = "人员名称不能为空") private String roubleName; /** * 编号 */ @Column(name = "rouble_ID") @ApiModelProperty("编号") @NotBlank(message = "编号不能为空") private String roubleID; } 帮我完成接口实现类和controller层,要求里面要有RoublePersonnelPoolRepository类并且extends BaseRepository<RoubleObjectPool,String>。 帮我写一套增删改查。
接口实现类和控制器层的代码如下:
RoublePersonnelPoolRepository.java:
```java
@Repository
public interface RoublePersonnelPoolRepository extends BaseRepository<RoubleObjectPool, String> {
}
```
RoublePersonnelPoolServiceImpl.java:
```java
@Service
public class RoublePersonnelPoolServiceImpl implements RoublePersonnelPoolService {
private final RoublePersonnelPoolRepository roublePersonnelPoolRepository;
@Autowired
public RoublePersonnelPoolServiceImpl(RoublePersonnelPoolRepository roublePersonnelPoolRepository) {
this.roublePersonnelPoolRepository = roublePersonnelPoolRepository;
}
@Override
public RoublePersonnelPool getById(String id) {
return roublePersonnelPoolRepository.findById(id).orElse(null);
}
@Override
public List<RoublePersonnelPool> getAll() {
return roublePersonnelPoolRepository.findAll();
}
@Override
public RoublePersonnelPool create(RoublePersonnelPool roublePersonnelPool) {
return roublePersonnelPoolRepository.save(roublePersonnelPool);
}
@Override
public RoublePersonnelPool update(String id, RoublePersonnelPool roublePersonnelPool) {
RoublePersonnelPool existingRoublePersonnelPool = getById(id);
if (existingRoublePersonnelPool != null) {
existingRoublePersonnelPool.setNumberId(roublePersonnelPool.getNumberId());
existingRoublePersonnelPool.setRoubleUnit(roublePersonnelPool.getRoubleUnit());
existingRoublePersonnelPool.setRoubleBumen(roublePersonnelPool.getRoubleBumen());
existingRoublePersonnelPool.setRoubleName(roublePersonnelPool.getRoubleName());
existingRoublePersonnelPool.setRoubleID(roublePersonnelPool.getRoubleID());
return roublePersonnelPoolRepository.save(existingRoublePersonnelPool);
}
return null;
}
@Override
public boolean delete(String id) {
RoublePersonnelPool existingRoublePersonnelPool = getById(id);
if (existingRoublePersonnelPool != null) {
roublePersonnelPoolRepository.delete(existingRoublePersonnelPool);
return true;
}
return false;
}
}
```
RoublePersonnelPoolController.java:
```java
@RestController
@RequestMapping("/rouble-personnel-pool")
public class RoublePersonnelPoolController {
private final RoublePersonnelPoolService roublePersonnelPoolService;
@Autowired
public RoublePersonnelPoolController(RoublePersonnelPoolService roublePersonnelPoolService) {
this.roublePersonnelPoolService = roublePersonnelPoolService;
}
@GetMapping("/{id}")
public ResponseEntity<RoublePersonnelPool> getById(@PathVariable String id) {
RoublePersonnelPool roublePersonnelPool = roublePersonnelPoolService.getById(id);
if (roublePersonnelPool != null) {
return ResponseEntity.ok(roublePersonnelPool);
}
return ResponseEntity.notFound().build();
}
@GetMapping
public ResponseEntity<List<RoublePersonnelPool>> getAll() {
List<RoublePersonnelPool> roublePersonnelPools = roublePersonnelPoolService.getAll();
if (!roublePersonnelPools.isEmpty()) {
return ResponseEntity.ok(roublePersonnelPools);
}
return ResponseEntity.noContent().build();
}
@PostMapping
public ResponseEntity<RoublePersonnelPool> create(@RequestBody RoublePersonnelPool roublePersonnelPool) {
RoublePersonnelPool createdRoublePersonnelPool = roublePersonnelPoolService.create(roublePersonnelPool);
if (createdRoublePersonnelPool != null) {
return ResponseEntity.ok(createdRoublePersonnelPool);
}
return ResponseEntity.badRequest().build();
}
@PutMapping("/{id}")
public ResponseEntity<RoublePersonnelPool> update(@PathVariable String id, @RequestBody RoublePersonnelPool roublePersonnelPool) {
RoublePersonnelPool updatedRoublePersonnelPool = roublePersonnelPoolService.update(id, roublePersonnelPool);
if (updatedRoublePersonnelPool != null) {
return ResponseEntity.ok(updatedRoublePersonnelPool);
}
return ResponseEntity.notFound().build();
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> delete(@PathVariable String id) {
boolean deleted = roublePersonnelPoolService.delete(id);
if (deleted) {
return ResponseEntity.noContent().build();
}
return ResponseEntity.notFound().build();
}
}
```
请注意,这只是一个基本的示例,你可能需要根据你的业务逻辑进行相应的修改和调整。
描述有两棵大小为n且以1为根的树,设为树1和树2,令根的深度为0,在树1上编号为x的点,在树2中编号为a[x]。定义点对(x,y)的价值为树1中x和y的最近公共祖先的深度+树2中a[x]和a[y]的最近公共祖先的深度。输出所有点对的价值的最大值。示例输入:3,[0,2,1,3],[0,0,1,1],[0,0,1,2]返回值:1备注:输入时给出n和三个数组a,b,c。a数组下标从0到n,意义如题,保证a[0]=0,a[1]~a[n]的数互不相同且1~n各只出现一次。b数组下标从0到n,为描述树1的数组,b[x]表示树1上x的父亲节点的编号,其中b[0]=b[1]=0.c数组下标从0到n,为描述树2的数组,c[x]表示树2上x的父亲节点的编号,其中c[0]=c[1]=0.输出一个整数,表示最大的价值。n<=100000,考虑到n很大的情况会使得运行效率变慢,请使用时间和空间复杂度较低的算法,并请按照以下模板进行实现: import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @param a int整型一维数组 * @param b int整型一维数组 * @param c int整型一维数组 * @return int整型 */ public int wwork (int n, int[] a, int[] b, int[] c) { // write code here } }
以下是Java实现的代码:
```java
import java.util.*;
public class Solution {
public int wwork(int n, int[] a, int[] b, int[] c) {
List<Integer>[] tree1 = buildTree(b);
List<Integer>[] tree2 = buildTree(c);
// 记录每个节点在树1和树2中的编号
int[] id1 = new int[n + 1];
int[] id2 = new int[n + 1];
int cnt = 0;
Queue<Integer> queue = new LinkedList<>();
queue.offer(1);
while (!queue.isEmpty()) {
int u = queue.poll();
id1[u] = ++cnt;
for (int v : tree1[u]) {
queue.offer(v);
}
}
cnt = 0;
queue.offer(1);
while (!queue.isEmpty()) {
int u = queue.poll();
id2[u] = ++cnt;
for (int v : tree2[u]) {
queue.offer(v);
}
}
// 预处理树1和树2的LCA
int[][] lca1 = buildLca(tree1, id1);
int[][] lca2 = buildLca(tree2, id2);
// 计算所有点对的价值并取最大值
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
int lca1Depth = lca1[id1[i]][id1[j]];
int lca2Depth = lca2[id2[a[i]]][id2[a[j]]];
ans = Math.max(ans, lca1Depth + lca2Depth);
}
}
return ans;
}
// 建立邻接表表示树
private List<Integer>[] buildTree(int[] parent) {
int n = parent.length;
List<Integer>[] tree = new List[n];
for (int i = 0; i < n; i++) {
tree[i] = new ArrayList<>();
}
for (int i = 1; i < n; i++) {
tree[parent[i]].add(i);
}
return tree;
}
// 建立LCA数组
private int[][] buildLca(List<Integer>[] tree, int[] id) {
int n = tree.length;
int maxDepth = (int) Math.floor(Math.log(n) / Math.log(2));
int[][] lca = new int[n][maxDepth + 1];
boolean[] visited = new boolean[n];
dfs(tree, id, lca, visited, 1, 0);
for (int k = 1; k <= maxDepth; k++) {
for (int i = 1; i < n; i++) {
lca[i][k] = lca[lca[i][k - 1]][k - 1];
}
}
return lca;
}
// DFS求LCA数组
private void dfs(List<Integer>[] tree, int[] id, int[][] lca, boolean[] visited, int u, int depth) {
visited[u] = true;
lca[id[u]][0] = u;
for (int v : tree[u]) {
if (!visited[v]) {
dfs(tree, id, lca, visited, v, depth + 1);
lca[id[v]][0] = u;
}
}
}
}
```
时间复杂度:$O(n\log n)$。
空间复杂度:$O(n)$。
阅读全文