「java数字合并」java数据合并

博主:adminadmin 2022-11-28 16:35:06 52

本篇文章给大家谈谈java数字合并,以及java数据合并对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

如何在java里java字符串数组合并成一个数组?

java里java字符串数组合并成一个数组方法如下:

//方法一 Arrays类

String[] a = {"A","B","C"};

String[] b = {"D","E"};

// ListString list = Arrays.asList(a);  --OK

// ListString list = Arrays.asList("A","B","C"); --OK

// list.add("F"); --UnsupportedOperationException

// list.remove("A"); --UnsupportedOperationException

// list.set(1,"javaee");--OK (因为是把数组转为集合,其本质还是数组,数组长度固定不变,但内容可以改变)

// 结论:虽然可以把数组转为集合,但是集合长度不能改变

List list = new ArrayList(Arrays.asList(a));

list.addAll(Arrays.asList(b));

String[] str = new String[list.size()];

list.toArray(str);

for(int x=0;xstr.length;x++){

System.out.print(str[x] + " ");

}

//方法二 循环遍历

// 两个数组合并

String[] str1 = {"Hello","world","java"};

String[] str2 = {"Veriable","syntax","interator"};

String[] newStr = new String[str1.length+str2.length];

//newStr = str1;数组是引用类型

for(int x=0;xstr1.length;x++){

newStr[x] = str1[x];

}

for(int y=0;ystr2.length;y++){

newStr[str1.length+y]=str2[y];

}

for(int y=0;ynewStr.length;y++){

System.out.println(newStr[y] + " ");

  }

// 方法三

String[] str1 = {"Hello","world","java"};

String[] str2 = {"Veriable","syntax","interator"};

int str1Length = str1.length;

int str2length = str2.length;

str1 = Arrays.copyOf(str1, str1Length+str2length);//数组扩容

System.arraycopy(str2, 0, str1, str1Length, str2length);

System.out.println(Arrays.toString(str1));

java怎么将2个数组的数据合并?

concat()方法是对字符串的操作,不是对整数或数组。

concat()用法:

String a="abc";

String b="edf";

String c=a.concat(b);

c的值为“abcdef"

数组可以用for循环合并:

public static void main(String[] args){

int a[]={1,7,9,11,13,15,17,19};

int b[]={2,4,6,8,10};

int aL=a.length;

int bL=b.length;

int lenght=aL+bL;

int[] c=new int[lenght];

for(int i=0;ilenght;i++){

if(iaL){//

c[i]=a[i];

}

else{

c[i]=b[i-aL];

}

}

for(int i=0;ic.length;i++){

System.out.print(c[i]+" ");

}

}

求java合并json数据的代码

我想了一下,但是得有一个前提,就是第一个json数组的size必须和第二个json数组的size相同,并且一一对应,否则将造成数组溢出。

如果是基于上面这个前提,那么实现的方法就简单了。

操作json对象,其实标准的方法是将实体类转换成json后再操作,我这里的话为了便捷直接使用谷歌的Gson来创建JsonObject了,其他的json依赖还有阿里巴巴的FastJson等等,看你平时用什么习惯。

引入Gson依赖:

dependency

groupIdcom.google.code.gson/groupId

artifactIdgson/artifactId

version2.8.0/version

/dependency

实现代码:

public class Main {

public static void main(String[] args) {

JsonArray jsonArray1 = new JsonArray();

JsonObject json11 = new JsonObject();

json11.addProperty("数据1", "0000");

json11.addProperty("数据2", "1111");

JsonObject json12 = new JsonObject();

json12.addProperty("数据1", "0000");

json12.addProperty("数据2", "1111");

JsonObject json13 = new JsonObject();

json13.addProperty("数据1", "0000");

json13.addProperty("数据2", "1111");

jsonArray1.add(json11);

jsonArray1.add(json12);

jsonArray1.add(json13);

System.out.println(jsonArray1);

JsonArray jsonArray2 = new JsonArray();

JsonObject json21 = new JsonObject();

json21.addProperty("数据3", "6666");

JsonObject json22 = new JsonObject();

json22.addProperty("数据3", "6666");

JsonObject json23 = new JsonObject();

json23.addProperty("数据3", "6666");

jsonArray2.add(json21);

jsonArray2.add(json22);

jsonArray2.add(json23);

System.out.println(jsonArray2);

//遍历json数组,按位取出对象

for (int i = 0; i jsonArray1.size(); i++) {

JsonObject json1 = jsonArray1.get(i).getAsJsonObject();

JsonObject json3 = jsonArray2.get(i).getAsJsonObject();

//遍历数据3内容,通过Entry获取数据3的key和value,并合并到数据1中

for (Map.EntryString, JsonElement item : json3.entrySet()) {

json1.addProperty(item.getKey(), item.getValue().getAsString());

}

}

System.out.println(jsonArray1);

}

}

整体思路为:遍历两个json数组,按位进行合并操作。合并时,遍历数据3的jsonObject,获取其key和value,并将其合并到数据1中即可。

运行结果:

java如何把两组数据合并?

String[] arr1 = new String[]{"A","B","C"};

String[] arr2 = new String[]{"a","b","c"};

for (int i = 0; i arr1.length; i++) {

arr1[i] = arr1[i].concat(arr2[i]);

}

java数字合并的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java数据合并、java数字合并的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-28,除非注明,否则均为首码项目网原创文章,转载请注明出处。