欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Java請求合并代碼,合并排序java代碼實現(xiàn)

怎么用java把多個list合并成一個?

下面的例子講了如何合并兩個java list, 用簡單的方法,而不用自己去造輪子,循環(huán)list 去做,用如下建檔方法:

成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比雙陽網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式雙陽網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋雙陽地區(qū)。費用合理售后完善,十載實體公司更值得信賴。

import java.util.ArrayList;import java.util.List;public class JoinList { ?

public static void main(String[] args) { ? ? ?

ListString listA = new ArrayListString();????????listA.add("A");

ListString listB = new ArrayListString();????????listB.add("B");

ListString listFinal = new ArrayListString

();????????listFinal.addAll(listA);????????listFinal.addAll(listB);

//same result????????//ListString listFinal = new ArrayListString

(listA);????????//listFinal.addAll(listB);

System.out.println("listA : " + listA);????????System.out.println("listB : " + listB);????????System.out.println("listFinal : " + listFinal);

}}

你可以看到結果如下:

listA : [A]listB : [B]listFinal : [A, B]

其實最關鍵的就是調用 list 的 addall 方法,apache 的 listUtil??, 里面有個 union 方法,其實也是用的 addall 方法。

如何使用java合并多個文件

使用java編程語言,對文件進行操作,合并多個文件,代碼如下:

import?static?java.lang.System.out;

import?java.io.FileInputStream;

import?java.io.FileOutputStream;

import?java.io.IOException;

import?java.nio.ByteBuffer;

import?java.nio.channels.FileChannel;

import?java.util.Arrays;

public?class?test?{

public?static?final?int?BUFSIZE?=?1024?*?8;

public?static?void?mergeFiles(String?outFile,?String[]?files)?{

FileChannel?outChannel?=?null;

out.println("Merge?"?+?Arrays.toString(files)?+?"?into?"?+?outFile);

try?{

outChannel?=?new?FileOutputStream(outFile).getChannel();

for(String?f?:?files){

FileChannel?fc?=?new?FileInputStream(f).getChannel();?

ByteBuffer?bb?=?ByteBuffer.allocate(BUFSIZE);

while(fc.read(bb)?!=?-1){

bb.flip();

outChannel.write(bb);

bb.clear();

}

fc.close();

}

out.println("Merged!!?");

}?catch?(IOException?ioe)?{

ioe.printStackTrace();

}?finally?{

try?{if?(outChannel?!=?null)?{outChannel.close();}}?catch?(IOException?ignore)?{}

}

}

//下面代碼是將D盤的1.txt?2.txt?3.txt文件合并成out.txt文件。

public?static?void?main(String[]?args)?{

mergeFiles("D:/output.txt",?new?String[]{"D:/1.txt",?"D:/2.txt",?"D:/3.txt"});

}

}

java中如何將兩個文件合并到另一個文件

java可以使用FileChannel快速高效地將多個文件合并到一起,以下是詳細代碼:

import?static?java.lang.System.out;??

import?java.io.FileInputStream;??

import?java.io.FileOutputStream;??

import?java.io.IOException;??

import?java.nio.ByteBuffer;??

import?java.nio.channels.FileChannel;??

import?java.util.Arrays;??

public?class?test?{??

public?static?final?int?BUFSIZE?=?1024?*?8;??

public?static?void?mergeFiles(String?outFile,?String[]?files)?{??

FileChannel?outChannel?=?null;??

out.println("Merge?"?+?Arrays.toString(files)?+?"?into?"?+?outFile);??

try?{??

outChannel?=?new?FileOutputStream(outFile).getChannel();??

for(String?f?:?files){??

FileChannel?fc?=?new?FileInputStream(f).getChannel();???

ByteBuffer?bb?=?ByteBuffer.allocate(BUFSIZE);??

while(fc.read(bb)?!=?-1){??

bb.flip();??

outChannel.write(bb);??

bb.clear();??

}??

fc.close();??

}??

out.println("Merged!!?");??

}?catch?(IOException?ioe)?{??

ioe.printStackTrace();??

}?finally?{??

try?{if?(outChannel?!=?null)?{outChannel.close();}}?catch?(IOException?ignore)?{}??

}??

}??

public?static?void?main(String[]?args)?{??

mergeFiles("D:/output.txt",?new?String[]{"D:/in_1.txt",?"D:/in_2.txt",?"D:/in_3.txt"});??

}??

}

求java合并json數(shù)據(jù)的代碼

我想了一下,但是得有一個前提,就是第一個json數(shù)組的size必須和第二個json數(shù)組的size相同,并且一一對應,否則將造成數(shù)組溢出。

如果是基于上面這個前提,那么實現(xiàn)的方法就簡單了。

操作json對象,其實標準的方法是將實體類轉換成json后再操作,我這里的話為了便捷直接使用谷歌的Gson來創(chuàng)建JsonObject了,其他的json依賴還有阿里巴巴的FastJson等等,看你平時用什么習慣。

引入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內容,通過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中即可。

運行結果:

文章題目:Java請求合并代碼,合并排序java代碼實現(xiàn)
文章網(wǎng)址:http://chinadenli.net/article44/dsidpee.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿建站網(wǎng)站設計虛擬主機網(wǎng)頁設計公司響應式網(wǎng)站面包屑導航

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化