# 问题内容:

I have two arrays:

  int[]a = {1, 2, 3};
int[]b = {4, 5, 6};


And result should be like below:

  int[]c = {"1":"4","2":"5", "3":"6"}


Any idea, how can i achieve that ?

## 问题评论:

Do you maybe want a Map<Integer, Integer>?
what is : in resultant array
int[]c = {"1" is not valid. "1" is a String which cannot be stored in an int.
An int[] array c cannot contain Strings, and please explain what you mean by the colon in this array – that’s not Java array syntax.
Because, in Java, you cannot resize an array, you will have to create a new array of appropriate length (a.length + b.length) first. Then copy each entry from the source arrays into the new array at its respective position. Hint: b[i] goes to the position immediately after a[i].

# 答案:

## 答案1:

Assuming the array has the same size for a and b and since you didnt specify the dict i used strings:

Map<String, String> map = new HashMap<String, String>();
for (int i=0; i < a.length; i++)
map.put(a[i], b[i]);


## 答案评论:

the if can be removed, the OP never mentioned anything about a condition
Sorry i got confused by the title, will edit.

## 答案2:

I’m guessing you want an array containing all the elements from the arrays a and b, which the following code provides:

final int[] c = new int[a.length + b.length];
for(int i = 0; i < a.length; i++){
c[i] = a[i];
c[i+1] = b[i];
}

System.out.println(Arrays.toString(c));


prints:

 [1, 4, 2, 5, 3, 6]


## 答案评论:

Well to be very specific: Array A contains: my headers. ( Name, Age ) Array B will contains there values: like (John,17) (Alex,21) I want to map my headers from A with these values in B, such that it will create a string per record: {“Name”:”John”,”Age”:”17″} {“Name”:”Alex”,”Age”:”21″} So on for further records as well.
– Prashant Varun
3 hours ago

## 答案3:

int a[] = {1, 2 , 3};
int b[]= {4,5,6};
Map<Integer ,Integer> map = new HashMap<Integer ,Integer> ();
for(int i=0;i<a.length;i++){
map.put(a[i],b[i]);
}
for (Integer key: map.keySet()) {
System.out.print(key + ":" + map.get(key));
}
`

## 答案评论:

This is same as above answer
@priyanka: Well to be very specific: Array A contains: my headers. ( Name, Age ) Array B will contains there values: like (John,17) (Alex,21) I want to map my headers from A with these values in B, such that it will create a string per record: {“Name”:”John”,”Age”:”17″} {“Name”:”Alex”,”Age”:”21″} So on for further records as well.
– Prashant Varun
3 hours ago

## 原文地址：

https://stackoverflow.com/questions/47752436/i-want-to-concatenate-two-arrays-in-java-such-that-below-condition-is-satisfied

0