国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Java中合并兩個(gè)數(shù)組的4種方法(How to Merge Two Arrays in Java)

這篇具有很好參考價(jià)值的文章主要介紹了Java中合并兩個(gè)數(shù)組的4種方法(How to Merge Two Arrays in Java)。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

int[] arr1={1, 2, 3, 4, 5, 6}; //first array
int[] arr2={7, 8, 9, 0}; //second array
int[] arr3={1, 2, 3, 4, 5, 6, 7, 8, 9, 0} //resultant array

There are following ways to merge two arrays:
1.Java arraycopy() method
2.Without using arraycopy() method
3.Java Collections
4.Java Stream API

1.Java arraycopy() method

Java arraycopy() is the method of System class which belongs to java.lang package.
It copies an array from the specified source array to the specified position of the destination array.
The number of elements copied is equal to the length argument.
Syntax:
public static void arraycopy(Object source, int source_position, Object destination, int destination_position, int length)
Parameters

  • source: It is a source array.
  • source_position: Starting point in the source array.
  • destination: It is a destination array.
  • destination_position: Starting position in the destination array.
  • length: The number of array elements to be copied

NullPointerException
It throws NullPointerException if the source or destination array is null. It also throws ArrayIndexOutOfBoundsException if:

  • source_position or destination_position or length is negative.
  • source_position+length is greater than the length of the source array, or destination_position+length is - greater than the length of the destination array.
public class A{
    public static void main(String[] args) {
        int[] firstArray = {23,45,12,78,4,90,1};        //source array
        int[] secondArray = {77,11,45,88,32,56,3};  //destination array
        int fal = firstArray.length;        //determines length of firstArray
        int sal = secondArray.length;   //determines length of secondArray
        int[] result = new int[fal + sal];  //resultant array of size first array and second array
        System.arraycopy(firstArray, 0, result, 0, fal);
        System.arraycopy(secondArray, 0, result, fal, sal);
        System.out.println(Arrays.toString(result));    //prints the resultant array
    }
}

[23, 45, 12, 78, 4, 90, 1, 77, 11, 45, 88, 32, 56, 3]

according to the specified positions and length.

public class A{
    public static void main(String[] args) {
        int source_arr [] = { 11,22,33,44,55,98,76,54,60};
        int dest_arr[] = {66,77,88,99,22,67,21,90,80,70};
        int sourcePos = 2;
        int destPos = 4;
        int len = 3;
//invoking arraycopy() method
        System.arraycopy(source_arr, sourcePos, dest_arr,destPos, len);
// Print elements of destination after
        System.out.print("Resultant array : ");
        for (int i = 0; i < dest_arr.length; i++)
            System.out.print(dest_arr[i] + " ");
    }
}

Resultant array : 66 77 88 99 33 44 55 90 80 70 

2.Without using arraycopy() method

Manually copy the each element of both arrays to mergedArray and convert that array into String by using toString() method of Array class.

public class A{
    public static void main(String[] args) {
        int[] firstArray = {56,78,90,32,67,12}; //initialized array
        int[] secondArray = {11,14,9,5,2,23,15};
        int length = firstArray.length + secondArray.length; //add the length of firstArray into secondArray
        int[] mergedArray = new int[length];    //resultant array
        int pos = 0;
        for (int element : firstArray) //copying elements of secondArray using for-each loop
        {
            mergedArray[pos] = element;
            pos++;              //increases position by 1
        }
        for (int element : secondArray) //copying elements of firstArray using for-each loop
        {
            mergedArray[pos] = element;
            pos++;
        }
        System.out.println(Arrays.toString(mergedArray));   //prints the resultant array  
    }
}

[56, 78, 90, 32, 67, 12, 11, 14, 9, 5, 2, 23, 15]

3.Java Collections

Using the Arrays.asList() method.
Now we have created the list view of str2 and added all the elements of str2 into the list.
Again perform conversion from list to array and store the resultant array into str3 variable.

public class A{
    public static void main(String[] args) {
        String str1[] = { "A", "E", "I" };          //source array
        String str2[] = { "O", "U" };               //destination array
        List list = new ArrayList(Arrays.asList(str1)); //returns a list view of an array
//returns a list view of str2 and adds all elements of str2 into list
        list.addAll(Arrays.asList(str2));
        Object[] str3 = list.toArray();         //converting list to array
        System.out.println(Arrays.toString(str3));  //prints the resultant array
    }
}

[A, E, I, O, U]

4.Java Stream API

Stream.of() method
The Stream.of() method of Stream interface returns a sequential ordered stream whose elements are the values.

Syntax
static Stream of(T…values)
Explain:Where MT is the type of stream elements. The method accepts values (elements of the new stream).

flatMap() method
The flatMap() method is the method of Stream interface. It returns a stream consisting of the result.
Syntax
Stream flatMap(Function<? Super T, ? extends Stream<? Extends R>> mapper)
Explain:Where R is the element type of new stream. The method accepts a mapper (a function to apply to each element which produces a stream of new values) as a parameter.

toArray() method
The toArray() method of Stream interface returns an array containing the elements of the stream.
Syntax
Object[] toArray()文章來源地址http://www.zghlxwxcb.cn/news/detail-513345.html

public class A{
    // function to merge two arrays
    public static <T> Object[] mergeArray(T[] arr1, T[] arr2)
    {
        return Stream.of(arr1, arr2).flatMap(Stream::of).toArray();
    }
    public static void main (String[] args)
    {
        Integer[] firstArray = new Integer[]{13,12,11,6,9,3}; //source array
        Integer[] secondArray = new Integer[]{78,34,56,67,2,11,7}; //destination array
        Object[] mergedArray = mergeArray(firstArray,secondArray); //merged array
        System.out.println("Merged array: "+ Arrays.toString(mergedArray));
    }
}

Merged array: [13, 12, 11, 6, 9, 3, 78, 34, 56, 67, 2, 11, 7]

到了這里,關(guān)于Java中合并兩個(gè)數(shù)組的4種方法(How to Merge Two Arrays in Java)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【Java基礎(chǔ)教程】(三十八)常用類庫篇 · 第八講:數(shù)組操作類——解析Arrays類中的全部操作方法,解鎖Java數(shù)組操作技巧~

    【Java基礎(chǔ)教程】(三十八)常用類庫篇 · 第八講:數(shù)組操作類——解析Arrays類中的全部操作方法,解鎖Java數(shù)組操作技巧~

    前言:在學(xué)習(xí)本文之前,應(yīng)該先學(xué)習(xí)并清楚了解Java基礎(chǔ)部分的數(shù)組相關(guān)的概念和知識。 若還不具備學(xué)習(xí)條件,請先前往學(xué)習(xí)數(shù)組部分的基礎(chǔ)知識: 《【Java基礎(chǔ)教程】(八)面向?qū)ο笃?· 第二講:Java 數(shù)組全面解析——?jiǎng)討B(tài)與靜態(tài)初始化、二維數(shù)組、方法參數(shù)傳遞、排序與轉(zhuǎn)

    2024年02月15日
    瀏覽(45)
  • git diff兩個(gè)分支有差異git merge卻顯示沒有可以合并的內(nèi)容

    git diff兩個(gè)分支有差異git merge卻顯示沒有可以合并的內(nèi)容

    problem: 用git diff可以發(fā)現(xiàn)兩個(gè)分支還是有很多不一樣的地方,可用git merge顯示not something we can merge 輸入gitk查看,發(fā)現(xiàn)preview已經(jīng)在這個(gè)分支前面了。。。雖然不太懂,但這樣是沒辦法將preview合并當(dāng)前分支的,只能這個(gè)分支合并preview。。。 解決:因?yàn)橐膊粫e的解決方法,因

    2024年02月12日
    瀏覽(39)
  • 【算法】Maximum Sum of Two Non-Overlapping Subarrays 兩個(gè)非重疊子數(shù)組的最大和

    問題 有一個(gè)整數(shù)數(shù)組nums,和2個(gè)整數(shù)firstlen,secondlen,要求找出2個(gè)非重疊子數(shù)組中的元素的最大和,長度分別是firstlen,secondlen。 不限制2個(gè)子數(shù)組的先后順序。 firstlen,secondlen的范圍 [ 1 , 1000 ] [1,1000] [ 1 , 1000 ] firstlen+secondlen的范圍 [ 2 , 1000 ] [2,1000] [ 2 , 1000 ] f i r s t l e n ,

    2023年04月27日
    瀏覽(354)
  • java合并數(shù)組的方法

    java合并數(shù)組的方法

    ? 在 Java中,數(shù)組是一種重要的數(shù)據(jù)結(jié)構(gòu),在 Java中數(shù)組的操作方式有兩種,一種是直接使用數(shù)組來操作,另一種是通過引用計(jì)數(shù)或者雙指針對數(shù)組進(jìn)行操作。對于直接使用數(shù)組來操作的方式,我們可以通過兩個(gè)方法來實(shí)現(xiàn)。 一種是將數(shù)組作為參數(shù)傳遞給方法,然后在方法中

    2024年02月02日
    瀏覽(25)
  • 【面試經(jīng)典150 | 數(shù)組】合并兩個(gè)有序數(shù)組

    【面試經(jīng)典150 | 數(shù)組】合并兩個(gè)有序數(shù)組

    本專欄專注于分析與講解【面試經(jīng)典150】算法,兩到三天更新一篇文章,歡迎催更…… 專欄內(nèi)容以分析題目為主,并附帶一些對于本題涉及到的數(shù)據(jù)結(jié)構(gòu)等內(nèi)容進(jìn)行回顧與總結(jié),文章結(jié)構(gòu)大致如下,部分內(nèi)容會有增刪: Tag:介紹本題牽涉到的知識點(diǎn)、數(shù)據(jù)結(jié)構(gòu); 題目來源:

    2024年02月09日
    瀏覽(22)
  • 力扣_數(shù)組26—合并兩個(gè)有序數(shù)組

    給你兩個(gè)按 非遞減順序 排列的整數(shù)數(shù)組 nums1 和 nums2,另有兩個(gè)整數(shù) m 和 n ,分別表示 nums1 和 nums2 中的元素?cái)?shù)目。 請你 合并 nums2 到 nums1 中,使合并后的數(shù)組同樣按 非遞減順序 排列。 注意:最終,合并后數(shù)組不應(yīng)由函數(shù)返回,而是存儲在數(shù)組 nums1 中。為了應(yīng)對這種情況,

    2024年01月21日
    瀏覽(23)
  • leetcode Median of Two Sorted Arrays

    Given two sorted arrays? nums1 ?and? nums2 ?of size? m ?and? n ?respectively, return? the median ?of the two sorted arrays. The overall run time complexity should be? O(log (m+n)) . Example 1: Example 2: Constraints: nums1.length == m nums2.length == n 0 = m = 1000 0 = n = 1000 1 = m + n = 2000 -106 = nums1[i], nums2[i] = 106

    2023年04月08日
    瀏覽(25)
  • 合并兩個(gè)有序數(shù)組(簡單)

    合并兩個(gè)有序數(shù)組(簡單)

    給你兩個(gè)按 非遞減順序 排列的整數(shù)數(shù)組 nums1 和 nums2 ,另有兩個(gè)整數(shù) m 和 n ,分別表示 nums1 和 nums2 中的元素?cái)?shù)目。請你 合并 nums2 到 nums1 中,使合并后的數(shù)組同樣按 非遞減順序 排列。 注意: 最終,合并后數(shù)組不應(yīng)由函數(shù)返回,而是存儲在數(shù)組 nums1 中。為了應(yīng)對這種情況

    2024年01月18日
    瀏覽(17)
  • leetcode 88 合并兩個(gè)有序數(shù)組

    題目描述: 給你兩個(gè)按 非遞減順序 排列的整數(shù)數(shù)組?nums1 和 nums2,另有兩個(gè)整數(shù) m 和 n ,分別表示 nums1 和 nums2 中的元素?cái)?shù)目。 請你 合并 nums2 到 nums1 中,使合并后的數(shù)組同樣按 非遞減順序 排列。 注意:最終,合并后數(shù)組不應(yīng)由函數(shù)返回,而是存儲在數(shù)組 nums1 中。為了應(yīng)

    2024年02月03日
    瀏覽(19)
  • LeetCode 0088. 合并兩個(gè)有序數(shù)組

    力扣題目鏈接:https://leetcode.cn/problems/merge-sorted-array/ 給你兩個(gè)按 非遞減順序 排列的整數(shù)數(shù)組? nums1 和 nums2 ,另有兩個(gè)整數(shù) m 和 n ,分別表示 nums1 和 nums2 中的元素?cái)?shù)目。 請你 合并 nums2 到 nums1 中,使合并后的數(shù)組同樣按 非遞減順序 排列。 注意: 最終,合并后數(shù)組不應(yīng)由

    2024年02月13日
    瀏覽(31)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包