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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java怎么将2个数组的数据合并?相关的知识,希望对你有一定的参考价值。

concat怎么用?
int a[]=1,7,9,11,13,15,17,19;
int b[]=2,4,6,8,10;

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;i<lenght;i++)
if(i<aL)//
c[i]=a[i];

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


for(int i=0;i<c.length;i++)
System.out.print(c[i]+" ");

参考技术A

不记得有可以合并两个数组的方法,感觉基本只能手动了,如:

int index=0;
int c[]=new int[a.length+b.length];
for(int i=0;i<a.length;i++)
    c[index++]=a[i];

for(int i=0;i<b.length;i++)
   c[index++]=b[i];

追问

运行错误

追答public class Test 

 public static void main(String[] args) 

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

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

  int index=0; 

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

  for(int i=0;i<a.length;i++) 

      c[index++]=a[i]; 

   

  for(int i=0;i<b.length;i++) 

     c[index++]=b[i]; 

  

  //打印合并后的数组C

  for(int i=0;i<c.length;i++)

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

  

 

本回答被提问者和网友采纳
参考技术B int a[]=1,7,9,11,13,15,17,19;
int b[]=2,4,6,8,10;

ArrayList<Integer> alist=new ArrayList<Integer>(a.length+b.length);

for (int j = 0; j < a.length; j++)
alist.add(a[j]);

for (int k = 0; k < b.length; k++)
alist.add(b[k]);

int c[] =new int[alist.size()];
for(int i=0; i<alist.size();i++)

c[i]=alist.get(i);
参考技术C concat是String类型的,无法在integer型数组上使用

基于共同元素合并 2 个数组

【中文标题】基于共同元素合并 2 个数组【英文标题】:Merge 2 arrays based on a common element 【发布时间】:2021-12-05 21:18:12 【问题描述】:

所以我有一个拉入 2 个数组的 Laravel 控制器。

数组 1:

[
    
        "id":1,
        "created_at":null,
        "updated_at":null,
        "name":"The Darkroom",
        "description":"This is the room your parents warned you about",
        "image":"https:\/\/images.unsplash.com\/photo-1579662908513-50e1433a258a?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1234&q=80",
        "is_private":"0"
    ,
        "id":2,
        "created_at":null,
        "updated_at":null,
        "name":"Smoking & Cigars",
        "description":"Time to light up and enjoy a cigar!",
        "image":"https:\/\/images.unsplash.com\/photo-1617850136763-06bc0a9a089c?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2000&q=80",
        "is_private":"0"
    ,
        "id":3,
        "created_at":null,
        "updated_at":null,
        "name":"Humiliation",
        "description":"today is the day you are going to be exposed",
        "image":"https:\/\/images.unsplash.com\/photo-1571570261702-3d23956fa32e?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2600&q=80",
        "is_private":"1"
    ,
        "id":4,
        "created_at":null,
        "updated_at":null,
        "name":"Financial Domination",
        "description":"hand over your cash and say thank you Sir!",
        "image":"https:\/\/images.unsplash.com\/photo-1526304640581-d334cdbbf45e?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2200&q=80",
        "is_private":"0"
    ,"id":5,"created_at":null,"updated_at":null,"name":"Pups & Handlers","description":"Woof, woof, bark, sit","image":"https:\/\/images.unsplash.com\/photo-1506939754500-f27bc71fccd4?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2200&q=80","is_private":"1"]

数组 2:

"channels":"presence-chat.1":"user_count":1,"presence-chat.4":"user_count":1

最终数组应如下所示:

[
   
      "id":1
      "other data in the first array"
      "user_count": 1
   
]

我需要在控制器中做的是将数据合并到一个数组中。因此,理想情况下,取第二个数组,将 chat.ID 与第一个数组的 ID 匹配,然后将 user_count 添加到其中......好吧,你明白我的意思了。

我以前从未这样做过,所以我不知道如何最好地解决这个问题。任何帮助将永远感激!

【问题讨论】:

你能告诉我们你希望最终的数组如何作为一个例子吗? 【参考方案1】:

给你,这里假设presence-chat.4 表示id 为4 的频道:

<?php

$json1 = '["id":1,"created_at":null,"updated_at":null,"name":"The Darkroom","description":"This is the room your parents warned you about","image":"https:\/\/images.unsplash.com\/photo-1579662908513-50e1433a258a?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1234&q=80","is_private":"0","id":2,"created_at":null,"updated_at":null,"name":"Smoking & Cigars","description":"Time to light up and enjoy a cigar!","image":"https:\/\/images.unsplash.com\/photo-1617850136763-06bc0a9a089c?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2000&q=80","is_private":"0","id":3,"created_at":null,"updated_at":null,"name":"Humiliation","description":"today is the day you are going to be exposed","image":"https:\/\/images.unsplash.com\/photo-1571570261702-3d23956fa32e?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2600&q=80","is_private":"1","id":4,"created_at":null,"updated_at":null,"name":"Financial Domination","description":"hand over your cash and say thank you Sir!","image":"https:\/\/images.unsplash.com\/photo-1526304640581-d334cdbbf45e?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2200&q=80","is_private":"0","id":5,"created_at":null,"updated_at":null,"name":"Pups & Handlers","description":"Woof, woof, bark, sit","image":"https:\/\/images.unsplash.com\/photo-1506939754500-f27bc71fccd4?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2200&q=80","is_private":"1"]';
$json2 = '"channels":"presence-chat.1":"user_count":1,"presence-chat.4":"user_count":1';

// convert the json into php arrays
$array1 = json_decode($json1, true);
$array2 = json_decode($json2, true);

// we extract the user_counts from the second array
// and make the index of a new array the channel id with value user_count
$channel_counts = [];
foreach ($array2['channels'] as $chan_name => $count) 
    $channel_id = explode('.', $chan_name)[1];
    $user_count = $count['user_count'];
    $channel_counts[$channel_id] = $user_count;


// we pass this array by reference as we are modifying it
foreach ($array1 as &$channel) 
    $id = $channel['id'];
    if (isset($channel_counts[$id]))
        $channel['user_count'] = $channel_counts[$id];
    else
        $channel['user_count'] = 0;

unset($channel);

$final_json = json_encode($array1, JSON_PRETTY_PRINT);

echo $final_json;

结果:

[
    
        "id": 1,
        "created_at": null,
        "updated_at": null,
        "name": "The Darkroom",
        "description": "This is the room your parents warned you about",
        "image": "https:\/\/images.unsplash.com\/photo-1579662908513-50e1433a258a?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1234&q=80",
        "is_private": "0",
        "user_count": 1
    ,
    
        "id": 2,
        "created_at": null,
        "updated_at": null,
        "name": "Smoking & Cigars",
        "description": "Time to light up and enjoy a cigar!",
        "image": "https:\/\/images.unsplash.com\/photo-1617850136763-06bc0a9a089c?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2000&q=80",
        "is_private": "0",
        "user_count": 0
    ,
    
        "id": 3,
        "created_at": null,
        "updated_at": null,
        "name": "Humiliation",
        "description": "today is the day you are going to be exposed",
        "image": "https:\/\/images.unsplash.com\/photo-1571570261702-3d23956fa32e?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2600&q=80",
        "is_private": "1",
        "user_count": 0
    ,
    
        "id": 4,
        "created_at": null,
        "updated_at": null,
        "name": "Financial Domination",
        "description": "hand over your cash and say thank you Sir!",
        "image": "https:\/\/images.unsplash.com\/photo-1526304640581-d334cdbbf45e?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2200&q=80",
        "is_private": "0",
        "user_count": 1
    , ...snip
]

【讨论】:

所以这和你给出的代码一样工作得很好,但是当我用我用来抓取数组的代码替换 json2 时,它给了我错误 "json_decode(): Argument #1 ($json ) 必须是字符串类型,stdClass given" 但是如果我返回数组,它就会显示,就像我将它粘贴到我的问题中一样。所以是的,你有正确的答案我只是不知道为什么它不适合我 你是如何获得 JSON 数组的? 在数组失败的情况下......它正在使用推送器 HTTP API - $pusherUserCount = $pusher->getChannels(['filter_by_prefix' => 'presence-', 'info' => 'user_count']) - 正如我所说 - 如果我返回 $pusherUserCount 它会显示数组,就像你在上面使用它一样 好的,试试$array2 = $pusher-&gt;getChannels(&lt;snip&gt;)-&gt;channels,去掉json_decode 不,仍然没有这样做。我要乱搞,看看是什么原因造成的。就像我说的那样,我已经尝试过使用您发布的静态数组并且它有效,我已经获取了数组并将其在线转换为 PHP 数组并且有效。所以我不确定这里有什么区别。

以上是关于java怎么将2个数组的数据合并?的主要内容,如果未能解决你的问题,请参考以下文章

华为OJ平台——整形数组合并

如果第二个数组的项目包含数组的第一个对象的 id,则合并 Angular 8 中的两个对象数组

如何把2个数组合并为一个数组

二路归并排序(也叫合并排序)

1.合并两个数组,并保持仍然有序。2.删除合并后数组中的重复元素

基于共同元素合并 2 个数组