数组数据结构的使用与代码编写(二)
定义数组
Student students[]=new Student[3];
students[0]=new Student("张三",10);
students[1]=new Student("李四",11);
students[2]=new Student("王五",12);
for(Student s:students){
System.out.println(s);
}
String[] abc = new String[] { "abc", "acd", "add" };
String[] abd = new String[] {"acd", "cd", "de"};
另外一种方式定义数组
int arr2[];
// 定义一个数组,并且静态初始化
int arr[]=new int[]{1,2,3};
定义动态数组
Object[] pointX = null;
Object[] pointY = null;
String sql1 = "select t.* from place t";
List<IBean> points =query(sql1, covergeId);
pointX = new Object[points.size()];
pointY = new Object[points.size()];
for(int j=0;j<points.size();j++){
pointX[j]=points.get(j).get("X");
pointY[j]=points.get(j).get("Y");
}
String [] sqls = new String[sheet.getLastRowNum()];
condictionSql = "INSERT INTO "+templateTable+"("+paramkey.toString()+") VALUES( "+paramval.toString()+")";
}
sqls[i-1] = condictionSql;
数组方法
java字符串与数组转换
字符数组转字符串可以通过下面的方式:
char[] data = {'a', 'b', 'd'};
String s = new String(data);
1.1通过for循环
StringBuffer sb = new StringBuffer();
for(int i = 0; i < str.length;i++){
sb.append(str[i]);
}
String s = sb.toString();
1.2通过split方法截取字符串来获得数组
String[] split = joinId.split(",");
String str = "0,1,2,3,4,5";
String[] arr = str.split(","); // 用,分割
System.out.println(Arrays.toString(arr)); // [0, 1, 2, 3, 4, 5]
@RequestMapping (value="/user/showUserList")
public String showUserList(Model model,String ids){
String[] idsArray = ids.split(",");
if(ids!="") {
StringBuffer str = new StringBuffer();
for(int i=0;i<idsArray.length;i++) {
User user = userService.getUser(idsArray[i]);
if(null != user)
{
String name = user.getUserName();
str.append(name+',');
}
}
model.addAttribute("membersName", str);
}
if(ids=="") {
model.addAttribute("membersName", "");
}
model.addAttribute("members", ids);
return "select/user/showUserList";
}
1.3使用StringUtils的join方法数组转为字符串
String[] arr = { "0", "1", "2", "3", "4", "5" };
//数组转字符串 org.apache.commons.lang3.StringUtils
String str3 = StringUtils.join(arr); // 数组转字符串,其实使用的也是遍历
System.out.println(str3); // 012345
String str4 = StringUtils.join(arr, ","); // 数组转字符串(逗号分隔)(推荐)
System.out.println(str4); // 0,1,2,3,4,5
String.join()方法:String.join()是JDK8新增方法(无需引包)字符串类型的数组推荐使用,将数组以某拼接符拼接到一起形成新的字符串。
String location = glEmerg.getLOCATION();
String[] str = location.split(",");
String Xstr = str[0];
String Ystr = str[1];
String[] arr=new String[]{Xstr,Ystr};
String newLocation = String.join(",",arr);
Arrays工具类
Arrays.toString数组与集合转为字符串
public static void main(String[] args) {
int arr[]={1,7,3,8,2};
System.out.println(arr);
System.out.println("以字符串形式输出数组:"+Arrays.toString(arr));
Arrays.sort(arr); // 给数组排序
System.out.println("排序后的数组:"+Arrays.toString(arr));
System.out.println(Arrays.binarySearch(arr, 1));
Arrays.fill(arr, 0); // 将指定内容填充到数组中
System.out.println("填充数组后的字符串:"+Arrays.toString(arr));
}
使用ArrayUtils的toString方法
// 数组转字符串 org.apache.commons.lang3.ArrayUtils
String str2 = ArrayUtils.toString(arr, ","); // 数组转字符串(逗号分隔,首尾加大括号)
System.out.println(str2); // {0,1,2,3,4,5}
Arrays.asList将数组转化为list
add是将传入的参数作为当前List中的一个Item存储,即使你传入一个List也只会另当前的List增加1个元素
addAll是传入一个List,将此List中的所有元素加入到当前List中,也就是当前List会增加的元素个数为传入的List的大小
List<String> members = new ArrayList<String>(Arrays.asList(activity.getMembers().split(",")));
members.addAll(orgUserIds);
members = new ArrayList<>(new HashSet<>(joinMembers));
if(CollectionUtils.isEmpty(members))
{
return "";
}
return String.join(",", members);
遍历数组
//普通的数组遍历方式
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
//foreach方式
for(int j:arr){
System.out.println(j);
}
int arr[]=new int[3];
arr[0]=1; // 给数组元素赋值
arr[2]=6;
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
Collection的遍历方法
String[] strings = {"A", "B", "C", "D"};
Collection stringList = java.util.Arrays.asList(strings);
/* 开始遍历 */
for (Iterator itr = stringList.iterator(); itr.hasNext();) {
Object str = itr.next();
System.out.println(str);
}
/* 依次输出“A”、“B”、“C”、“D” */
foreach的方式获取数组
String[] orgString = {"001001","001002","001003","001004","001005","001006"};
List<PipeVO> rushCount = pipeMapper.showPipeList(vo);
List<PipeVO> zeroString = new ArrayList<PipeVO>();
for (String code : orgString) {
boolean hasCde = false;
}
Arrays工具类的其他方法
public static void main(String[] args) {
int arr[]={1,7,3,8,2};
System.out.println(arr);
System.out.println("以字符串形式输出数组:"+Arrays.toString(arr));
Arrays.sort(arr); // 给数组排序
System.out.println("排序后的数组:"+Arrays.toString(arr));
System.out.println(Arrays.binarySearch(arr, 1));
Arrays.fill(arr, 0); // 将指定内容填充到数组中
System.out.println("填充数组后的字符串:"+Arrays.toString(arr));
}
二维数组
/*int arr[][]=new int[][]{{1,2,3},{4,5,6},{7,8,9}};
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}*/
int [][]arr=new int[3][3];
arr[1][2]=3;
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
java判断数组是否包含某个值
1、java通过container判断字符串是否包含某个特殊的字符
String str = "abc";
boolean status = str.contains("a");
if(status){
System.out.println("包含");
}else{
System.out.println("不包含");
}
也可以判断集合或数组是否包含字符串
java数组转为list方法,判断数组是否包含某个元素
import org.apache.commons.lang.StringUtils;
String[] roleIdsArr = memberRoleIds.split(",");
Arrays.asList(roleIdsArr).contains("5afd389c10174e21af1680dd9f79f049");
使用Arrays类中asList()方法将数组转化为List()集合,再使用contains()方法判断数组中是否存在某个值
import org.apache.commons.lang.StringUtils;
String memberRoleIds = userRole.getRoleId();
String[] roleIdsArr = memberRoleIds.split(",");
boolean containRoleId = Arrays.asList(roleIdsArr).contains("5afd389c10174e21af1680dd9f79f049");
if(!containRoleId) {
}
String memberRoleIds = userRole.getRoleId();
String[] roleIdsArr = memberRoleIds.split(",");
String[] newRoleIdsArr = new String[roleIdsArr.length+1];
String memRolds = "";
boolean containRoleId = Arrays.asList(roleIdsArr).contains("5afd389c10174e");
if(!containRoleId) {
for(int mm=0;mm<=roleIdsArr.length-1;mm++) {
newRoleIdsArr[mm] =roleIdsArr[mm];
}
newRoleIdsArr[roleIdsArr.length]="5afd389c10174e21af1680dd9f79f049";
memRolds = StringUtils.join(newRoleIdsArr, ",");
String pointMemRolds = "'"+memRolds+"'";
upRoleSql = "UPDATE CT_USER SET ROLE_ID="+pointMemRolds+ "where ID_NUMBER="+pointIdNum;
jdbcTemplate.update(upRoleSql);
}
public Map<String, List<String>> showPipeList(PipeVO vo)
{
Map<String, List<String>> maps = new HashMap<String, List<String>>();
List<String> yearCovlist = new ArrayList<String>();
List<String> orglist = new ArrayList<String>();
String[] orgString = {"001001","001002","001003","001004","001005","001006"};
List<PipeVO> rushCount = pipeMapper.showPipeList(vo);
for (PipeVO pipeVO : rushCount) {
BigDecimal yearCov = new BigDecimal("0");
String counts = pipeVO.getRunUser();
String orgs = pipeVO.getDepOrder();
if(!Arrays.asList(orgString).contains(orgs)) {
continue;
}
String dayCov = pipeVO.getDayCovRate();
String eventCauseName = compareDic(orgs);
if(""!=eventCauseName&&(!eventCauseName.equals(""))) {
if(null!=counts&&null!=dayCov) {
if(counts.equals("0")) {
continue;
}
BigDecimal num1 = new BigDecimal(counts);
BigDecimal num2 = new BigDecimal(dayCov);
yearCov = num2.divide(num1,2, BigDecimal.ROUND_HALF_UP);
}
}
yearCovlist.add(eventCauseName);
orglist.add(yearCov.toString());
}
maps.put("yearCovlist", yearCovlist);
maps.put("orglist", orglist);
return maps;
}
2、java的indexOf方法,在一个字符串中寻找一个字的位置,同时也可以判断一个字符串中是否包含某个字符。
String str="sasdwewuiundmxcss";
System.out.println("d在字符串str中第一次出现的索引位置:"+str.indexOf('d'));
System.out.println("d在字符串str中第一次出现的索引位置,从索引5位置开始:"+str.indexOf('d',4));
String str1 = "abcdefg";
int result1 = str1.indexOf("a");
if(result1 != -1){
System.out.println("字符串str中包含子串“a”"+result1);
}else{
System.out.println("字符串str中不包含子串“a”"+result1);
}
具体使用:
执行删除多条数据的操作
js
var ids = $("#id").val();
var url=Leopard.getContextPath() +"/deleteTczb1.act";
var idstr = "";
for(var i=0;i<ids.length;i++){
idstr+=(","+ids[i])
}
var paramData={ids:idstr};
getData(url,paramData);
java后台
@RequestMapping("/deleteTczb")
@ResponseBody
public Boolean deleteCzb(HttpServletRequest request, HttpServletResponse response){
String ids = request.getParameter("ids");
String[] idArray = ids.replaceFirst(",", "").split(",");
String sql = "delete from SY_TYZJKXCZB t where P_ID in(";
for (String string : idArray) {
sql+=(",?");
}
sql+=")";
SqlUtil.getExecutor().execute(sql.replaceFirst(",", ""), idArray);
return true;
}
java很多情况下是通过stringBuffer来拼接字符串的
StringBuffer sql = new StringBuffer("select ID from TB where ID in (");
StringBuffer temp = new StringBuffer();
for (String ID : Id.split(",")) {
temp.append(",?");
}
sql.append(temp.toString().replaceFirst(",", ""));
sql.append(")");
标签:arr,String,System,数组,new,编写,数据结构,out From: https://blog.51cto.com/u_11837698/6081989