我想了一下,但是得有一個前提,就是第一個json數(shù)組的size必須和第二個json數(shù)組的size相同,并且一一對應(yīng),否則將造成數(shù)組溢出。
通山ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!
如果是基于上面這個前提,那么實現(xiàn)的方法就簡單了。
操作json對象,其實標(biāo)準(zhǔn)的方法是將實體類轉(zhuǎn)換成json后再操作,我這里的話為了便捷直接使用谷歌的Gson來創(chuàng)建JsonObject了,其他的json依賴還有阿里巴巴的FastJson等等,看你平時用什么習(xí)慣。
引入Gson依賴:
dependency
groupIdcom.google.code.gson/groupId
artifactIdgson/artifactId
version2.8.0/version
/dependency
實現(xiàn)代碼:
public class Main {
public static void main(String[] args) {
JsonArray jsonArray1 = new JsonArray();
JsonObject json11 = new JsonObject();
json11.addProperty("數(shù)據(jù)1", "0000");
json11.addProperty("數(shù)據(jù)2", "1111");
JsonObject json12 = new JsonObject();
json12.addProperty("數(shù)據(jù)1", "0000");
json12.addProperty("數(shù)據(jù)2", "1111");
JsonObject json13 = new JsonObject();
json13.addProperty("數(shù)據(jù)1", "0000");
json13.addProperty("數(shù)據(jù)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("數(shù)據(jù)3", "6666");
JsonObject json22 = new JsonObject();
json22.addProperty("數(shù)據(jù)3", "6666");
JsonObject json23 = new JsonObject();
json23.addProperty("數(shù)據(jù)3", "6666");
jsonArray2.add(json21);
jsonArray2.add(json22);
jsonArray2.add(json23);
System.out.println(jsonArray2);
//遍歷json數(shù)組,按位取出對象
for (int i = 0; i jsonArray1.size(); i++) {
JsonObject json1 = jsonArray1.get(i).getAsJsonObject();
JsonObject json3 = jsonArray2.get(i).getAsJsonObject();
//遍歷數(shù)據(jù)3內(nèi)容,通過Entry獲取數(shù)據(jù)3的key和value,并合并到數(shù)據(jù)1中
for (Map.EntryString, JsonElement item : json3.entrySet()) {
json1.addProperty(item.getKey(), item.getValue().getAsString());
}
}
System.out.println(jsonArray1);
}
}
整體思路為:遍歷兩個json數(shù)組,按位進行合并操作。合并時,遍歷數(shù)據(jù)3的jsonObject,獲取其key和value,并將其合并到數(shù)據(jù)1中即可。
運行結(jié)果:
具體如下:
java字符串?dāng)?shù)組合并,可以使用array.copy復(fù)制方法,如下代碼:
package com.qiu.lin.he;
import java.text.ParseException;
import java.util.Arrays;
public class Ceshi {
public static void main(String[] args) throws ParseException {
String[] str1 = { "J", "a", "v", "a", "中" };
String[] str2 = { "如", "何", "把", "兩", "個", "數(shù)", "組", "合", "并", "為",
"一", "個" };
int strLen1 = str1.length;// 保存第一個數(shù)組長度
int strLen2 = str2.length;// 保存第二個數(shù)組長度
str1 = Arrays.copyOf(str1, strLen1 + strLen2);// 擴容
System.arraycopy(str2, 0, str1, strLen1, strLen2);// 將第二個數(shù)組與第一個數(shù)組合并
System.out.println(Arrays.toString(str1));// 輸出數(shù)組
}
}
public class IntTest {
static int joint(int x, int y) {
// 為提高字符串拼接的效率,使用StringBuilder而不使用String
StringBuilder sb = new StringBuilder();
sb = sb.append(x);// 在字符串結(jié)尾添加入x
sb = sb.append(y);// 在字符串結(jié)尾添加入y
int result = -1;
try {
result = Integer.valueOf(sb.toString());// 將得到的字符串轉(zhuǎn)為int類型
} catch (NumberFormatException e) {
}
// 返回-1表示操作失敗
return result;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int result = joint(2, 4);// 調(diào)用函數(shù)
System.err.println(result);// 測試結(jié)果
}
}
pre t="code" l="java"int[][] number = {{1,2,3},{4,5,6},{7,8,9}};
int[][] newnumber = new int[number[0].length][number.length];
for(int i = 0;inumber.length;i++){
for(int j = 0;jnumber[i].length;j++){
newnumber[i][j] = number[j][i];
}
}
System.out.println("This is new Array");
for(int i = 0;inumber.length;i++){
for(int j = 0;jnumber[i].length;j++){
System.out.print(newnumber[i][j]+" ");
}
System.out.println("");
}
System.out.println("This is old Array");
for(int i = 0;inumber.length;i++){
for(int j = 0;jnumber[i].length;j++){
System.out.print(number[i][j]+" ");
}
System.out.println("");
}
當(dāng)前題目:java合并數(shù)字的代碼 java合并數(shù)字的代碼怎么用
分享網(wǎng)址:http://chinadenli.net/article42/hgdiec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、建站公司、虛擬主機、動態(tài)網(wǎng)站、服務(wù)器托管、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)