「java两个数组的并集」java两个数组拼接

博主:adminadmin 2023-03-18 08:45:09 398

今天给各位分享java两个数组的并集的知识,其中也会对java两个数组拼接进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java中怎么合并两个数组 简单明了的

int[] arr1 = {1,2,3,4,11};

int[] arr2 = {6,7,8,9,10};

int newLength = arr1.length + arr2.length;

int[] arr_target = new int[newLength];

//参数:源数组,源数组起始位置,目标数组,目标数组起始位置,复制长度

System.arraycopy(arr1, 0, arr_target, 0, arr1.length);

System.arraycopy(arr2, 0, arr_target, arr1.length, arr2.length);

//输出合并后数组

for (int i : arr_target) {

System.out.println(i);

}

//排序

Arrays.sort(arr_target);

//输出排序数组

for (int i : arr_target) {

System.out.println(i);

}

//逆序

int[] arr_reverse = new int[newLength];

int flag = 0;

for (int i : arr_target) {

arr_reverse[newLength - flag - 1] = i;

flag++;

}

//输出逆序数组

for (int i : arr_reverse) {

System.out.println(i);

}

数组合并不一定非得遍历

具体的输出题主自己再修改吧

在JAVA中如何求两个数组的并集

public static void main(String[] args)

{

Integer[] m = { 1, 2, 3, 4, 5 };

Integer[] n = { 3, 4, 6 };

Integer[] b = getB(m, n); for (Integer i : b)

{

System.out.println(i);

}

}

private static Integer[] getB(Integer[] m, Integer[] n)

{ // 将数组转换为set集合

SetInteger set1 = new HashSetInteger(Arrays.asList(m));

SetInteger set2 = new HashSetInteger(Arrays.asList(n)); // 合并两个集合 set1.addAll(set2);

Integer[] arr = {}; return set1.toArray(arr);

}

JAVA怎么合并两个数组

三种字符数组合并的方法

public static String[] getOneArray() {

  String[] a = { "0", "1", "2" };

  String[] b = { "0", "1", "2" };

  String[] c = new String[a.length + b.length];

  for (int j = 0; j  a.length; ++j) {

   c[j] = a[j];

  }

  for (int j = 0; j  b.length; ++j) {

   c[a.length + j] = b[j];

  }

  return c;

 }

 public static Object[] getTwoArray() {

  String[] a = { "0", "1", "2" };

  String[] b = { "0", "1", "2" };

  List aL = Arrays.asList(a);

  List bL = Arrays.asList(b);

  List resultList = new ArrayList();

  resultList.addAll(aL);

  resultList.addAll(bL);

  Object[] result = resultList.toArray();

  return result;

 }

 public static String[] getThreeArray() {

  String[] a = { "0", "1", "2", "3" };

  String[] b = { "4", "5", "6", "7", "8" };

  String[] c = new String[a.length + b.length];

  System.arraycopy(a, 0, c, 0, a.length);

  System.arraycopy(b, 0, c, a.length, b.length);

  return c;

 }

Reference: 

Java中如何把两个数组合并为一个

import java.util.Arrays;

//Java中如何把两个数组合并为一个

public class gog {

public static void main(String[] args) {

String [] str1 = {"J","a","v","a","中"};

String [] str2 = {"如","何","把","两","个","数","组","合","并","为","一","个"};

int strLen1=str1.length;//保存第一个数组长度

int strLen2=str2.length;//保存第二个数组长度

str1= Arrays.copyOf(str1,strLen1+ strLen2);//扩容

System.arraycopy(str2, 0, str1, strLen1,strLen2 );//将第二个数组与第一个数组合并

System.out.println(Arrays.toString(str1));//输出数组

}

}

关于java两个数组的并集和java两个数组拼接的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。