java中怎么数组初始化?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中怎么数组初始化?相关的知识,希望对你有一定的参考价值。
参考技术A 一维数组\\x0d\\x0a \\x0d\\x0a1) \\x0d\\x0aint[] a; //声明,没有初始化\\x0d\\x0a\\x0d\\x0a2) \\x0d\\x0aint[] a=new int[5]; //初始化为默认值,int型为0 \\x0d\\x0a \\x0d\\x0a3) \\x0d\\x0aint[] a=1,2,3,4,5; \\x0d\\x0a//初始化为给定值\\x0d\\x0a4) \\x0d\\x0aint[] a=new int[]1,2,3,4,5; //同(3) \\x0d\\x0a \\x0d\\x0aint[] a=new int[5]1,2,3,4,5; \\x0d\\x0a//错误,如果提供了数组初始化操作,则不能定义维表达式\\x0d\\x0a5) \\x0d\\x0aint[] a; \\x0d\\x0aa=new int[5]; //正确,同(2)一样\\x0d\\x0a \\x0d\\x0aint[] a; \\x0d\\x0a \\x0d\\x0aa=1,2,3,4,5; //错误数组常量只能在初始化操作中使用,如(3)\\x0d\\x0a\\x0d\\x0a6) int a[]; \\x0d\\x0a a[0]=1; \\x0d\\x0a//错误,因为数组没有初始化,不能赋值a[1]=2; 二维数组\\x0d\\x0a \\x0d\\x0a1) \\x0d\\x0aint[][] a; \\x0d\\x0a//声明,没有初始化\\x0d\\x0a\\x0d\\x0a2) int[][] a=new int[2][3]; \\x0d\\x0a//初始化为默认值,int型为0 \\x0d\\x0a \\x0d\\x0a3) \\x0d\\x0aint[][] a=1,2,2,3,3,4; \\x0d\\x0a//初始化为给定值\\x0d\\x0aint[][] a=1,2,2,3,3,4,5; \\x0d\\x0a//没有错,数组空间不是连续分配的,所以不要求每一维的大小相同\\x0d\\x0a4) \\x0d\\x0aint[][] a=new int[2][]; \\x0d\\x0aa[0]=new int[3]; \\x0d\\x0a//a[0]其实就是一个数组a[1]=new int[4]; \\x0d\\x0a//每一维的大小可以不一样\\x0d\\x0a; \\x0d\\x0a \\x0d\\x0a5) \\x0d\\x0aint[][] a=new\\x0d\\x0aint[][]1,2,2,3,3,4,5; \\x0d\\x0a//同(3)编辑页面数据初始化时,一条数据中的某个字段,java后台怎么返回给前端数组
/** * 获取数据 */ @Override public StuInfo getStuById(Integer id) StuInfo info = stuInfoDao.getStuById(id); return info; info中有个字段A的值是这样的 A=1,2,3,4我想让A的值返回给前端是这样的 "A":["1","2","3","4"]跟下图字段sites的值一样,求大佬帮忙~~~~~~~~~
spring方式
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
@SpringBootApplication
@Log4j2
@RestController
public class LearnConcurrencyApplication
public static void main(String[] args)
SpringApplication.run(LearnConcurrencyApplication.class, args);
@GetMapping("/")
public String test()
log.warn("Receive request.");
return "test";
@GetMapping("/a")
public Map<String,Object> getA()
StudentInfo info = new StudentInfo();
//方式1,假设字段a是字符串
// info.setA("1,2,3,4,5,6");
// Map<String,Object> result = new HashMap<>();
// result.put("A",Arrays.asList(info.getA().split(",")));
// return result;
//假设A是list
List<String> list = Arrays.asList("1,2,3,4,5,6".split(","));
info.setA1(list);
Map<String,Object> result = new HashMap<>();
result.put("A",info.getA1());
return result;
@Bean
public ServletRegistrationBean servletRegistration()
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.addUrlMappings("/sa");
servletRegistrationBean.setServlet(new MyServlet());
return servletRegistrationBean;
servlet方式,注意依赖jackson,jar包
package com.learn;import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author lixiaosuo
* @since 2019-04-12 10:05
*/
public class MyServlet extends HttpServlet
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
StudentInfo info = new StudentInfo();
List<String> list = Arrays.asList("1,2,3,4,5,6".split(","));
info.setA1(list);
Map<String,Object> result = new HashMap<>();
result.put("A",info.getA1());
//依赖jackson
ObjectMapper mapper = new ObjectMapper();
String json =mapper.writeValueAsString(result);
resp.setContentType("application/json;charset=utf-8");
resp.setCharacterEncoding("utf-8");
resp.getWriter().write(json);
resp.getWriter().flush();
resp.getWriter().close();
辅助类
package com.learn;import java.util.List;
/**
* @author lixiaosuo
* @since 2019-04-12 09:46
*/
public class StudentInfo
public String a;
public List<String> a1;
public String getA()
return a;
public void setA(String a)
this.a = a;
public List<String> getA1()
return a1;
public void setA1(List<String> a1)
this.a1 = a1;
spring实现,输出结果:
servlet,实现输出结果
参考技术A按你的描述,A是用逗号隔开的字符串,应该先用split方法转成字符串数组,然后放在JSON里,直接toString返回前端即可
追问那怎么把obj.get("a")的值放到StuInfo的b字段里呢,返回是个StuInfo
追答b字段的格式应该是数组吧
info.setB(obj.get("a"))
StuInfo类中有个属性A,类型是List<String>
private List<String> A;以上是关于java中怎么数组初始化?的主要内容,如果未能解决你的问题,请参考以下文章