Java project, based on the spring framework to add elements to the array method

Project scenario:

提示:这里简述项目相关背景:

We mainly consider two situations:
1. The length of the array is determined
2. The length of the array is not determined


Problem Description

提示:问题简述:

Add new elements to an array


Method summary:

Hint: The solution is as follows:

1. If we know the definite length of the array, and the data added to the array is definite, then we can directly define a new array, specify the length of the new array, and assign the value directly.

示例代码:
@Test
public void arr(){
        String[] arr1 = {"a","b","c"};
       String[] arr2 = new String[arr1.length+2];
       for (int i = 0; i < arr1.length; i++) {
           arr2[i] = arr1[i];
      }
    //直接赋值
   arr2[arr1.length] = "d";
   arr2[arr1.length+1] = "e";
   //输出结果
   System.out.println(Arrays.toString(arr2));
 
   }

运行结果:
[a, b, c, d]

2. If we know the definite length of the array and the data added to the array is definite, then we can use the add() method in arrayList to add new data to the array. Therefore, we first need to convert the array to arrayList. After the increase, we can convert the arrayList into an array.

示例代码如下:
写法一:
@Test
    public void  arr(){
 
       String[] arr1 = {"a","b","c"};
      //   直接将 数组  转为  arrayList
       List<String> list = new ArrayList<>(Arrays.asList(arr1));
      // 加入新的元素
       list.add(1,"d");
        //  按照 合并时候的 list 大小定义一个新的 数组
       String[] arr2 = new String[list.size()];
       // arrayList   转为数组
        list.toArray(arr2);
       // 输出结果
      System.out.println(Arrays.toString(arr2));
 
   }

写法二:
@Test
    public void arr(){

        String[] arr1 = {"a","b","c"};
        // arr 转为  arrayList
        List<String> arrList = Arrays.asList(arr1);
        //定义新的  list
        List<String> list = new ArrayList<>();
          list.addAll(arrList );
          list.add(1,"d");
          list.add(1,"e");
        String[] arr2 = new String[list.size()];
         list.toArray(arr2);
         //运行结果
        System.out.println(Arrays.toString(arr2));
    }
运行结果:
[a, b, c,d]

3. If we are not sure about the actual length and the added elements, then we cannot specify the size of the array or the length of the list, so we need to use other methods to solve it. Below we use StringBuffer

示例代码:
List<SysDeptEntity> deptList = sysDeptService.list(new QueryWrapper<SysDeptEntity>().eq("type", 0).eq("parent_id", getCompanyId().intValue()));
            if(deptList.size() > 0) {
                //定义一个新的  list ,不指定长度
                List<String> comIds = new ArrayList<>();
                for (SysDeptEntity sysDept : deptList) {
                    comIds.add(sysDept.getDeptId().toString());
                }
                comIds.add(getCompanyId().toString());
                //arraylist  转 字符串
                StringBuffer resultStr = new StringBuffer();  // 利用StringBuffer将arraylist转为string
                for (int i = 0; i < comIds.size(); i++) {
                    if(i < comIds.size()-1){
                        resultStr.append(comIds.get(i) + ",");
                    }else{
                        resultStr.append(comIds.get(i));
                    }

                  }
                }
                String  comIdss=resultStr.toString();
                System.out.println(comIdss);
运行结果:
1,2,3
注意:
1、append()方法在被选元素的结尾(仍然在内部)插入指定内容。
2、提示:如需在被选元素的开头插入内容,请使用prepend()方法。
3、提示:append()和appendTo()方法执行的任务相同。不同之处在于:内容的位置和选择器。

Note: In actual use, judgment should be made according to the actual situation, and it is necessary to clarify what type of data we will eventually get.

Guess you like

Origin blog.csdn.net/YHLSunshine/article/details/130229178