1、dade文件
package model;
public class dade {
private int id;
private String name;
public dade() {
}
public dade(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2、AdminController文件文章来源:https://www.toymoban.com/news/detail-816187.html
package com.controller;
import com.myThread.AdminThread;
import com.myThread.MyCallable;
import com.myThread.MyRunnable;
import model.dade;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.concurrent.*;
//上面引入*,所以这个可以注销
//import java.util.concurrent.ConcurrentHashMap;
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController{
@GetMapping("/{id}")
public ArrayList findById(@PathVariable Long id) throws InterruptedException {
//可以添加任意类型数组
//ArrayList list = new ArrayList();
//加上类型限制,只能写这一种类型,jdk8后后面这个ArrayList可以不写<>,会引用前面这个
ArrayList<dade> list = new ArrayList();
dade da = new dade(1,"'dade'");
dade da1 = new dade(2,"dade333");
list.add(da);
list.add(da1);
for(int i=0;i<list.size();i++){
dade ds = list.get(i);
System.out.print(ds.getId()+"----"+ds.getName()+"\n");
}
return list;
}
}
文章来源地址https://www.toymoban.com/news/detail-816187.html
到了这里,关于java数组ArrayList(存对象)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!