简体   繁体   中英

Combining two different arrays in java

I really wanted to combine my two arrays and I really do not know what is wrong with my code it keeps giving me this results:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
    at javaDay3.ArrayExpanding.main(ArrayExpanding.java:17)

The results I want to view is :

    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0

Please help me to find what is wrong with my code: I wanted to combine the two arrays manually using loops

package javaDay3;    
public class ArrayExpanding {    
    public static void main(String[] args) {            
        int ages [] = {0,1,2,3,4,5,6,7,8,9}; // my first array
        for( int i = 0; i < ages.length; i++) {         
         int temp [] = new int [20];// my bigger and 2nd array
         for(int ix = 0; ix < temp.length; ix++) {          
          for(int ixx = 0; ixx <= temp.length; ixx++) {
            temp [0] = ages [0] ;               
            System.out.println(temp[ixx]);
          }
         }
        }
      }
}

should I add or remove something please help me I am using Eclipse and taking Java

You could try this method:

static int[] addElement(int[] a, int e) {
    a  = Arrays.copyOf(a, a.length + 1);
    a[a.length - 1] = e;
    return a;
}

You give it the List (a) and the element you want to add (e) and it returns the list with the added Element.

If you want to do it for multiple items you can just loop it, something like this:

for(int i = 0; i < ages.length; i++) {
    addElement(temp, ages[i]);
}

This is what you want,

public static void main(String[] args) {

        int ages[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        for (int i = 0; i < ages.length; i++) {
            // System.out.println(ages[i]);
        }

        int temp[] = new int[20];
        for (int ix = 0; ix < temp.length; ix++) {

        }

        for (int ixx = 0; ixx < temp.length; ixx++) {
        if (ages.length > ixx) {
            temp[ixx] = ages[ixx];
        }
        System.out.println(temp[ixx]);
    }

    }

output

0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
0
0

If you want to put all element in ages[] to temp[] , you can follow these steps.

  1. let say you have two arrays as follows.

     int ages[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int temp[] = new int[20]; 

2.Then iterate the ages array and assign value of each element to temp array

    for(int i = 0; i < ages.length; i++) {
        temp[i]=ages[i];
    }

3. Now your temp array contains what you want. You can print temp array either

    for(int i=0;i<temp.length;i++){
        System.out.println(temp[i]);
    }

Or

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

Eg:

    int ages[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int temp[] = new int[20];
    for (int i = 0; i < ages.length; i++) {
        temp[i]=ages[i];
    }
    for(int i=0;i<temp.length;i++){
        System.out.println(temp[i]);
    }
  for(int ix = 0; ix < temp.length; ix++) 
  {
    if(ages.length>ix)//ages having lengh 10
        temp [ix] = ages [ix] ;
        else
        temp [ix] = 0 ;//if ages length exceeds
        System.out.println(temp[ix]);       
  }

Output:

0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
0
0

Use arraycopy method from System class.

int[] ages = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] temp = new int[20];
System.arraycopy(ages, 0, temp, 0, ages.length);
System.out.println(Arrays.toString(temp));

To copy from one array to another, loop through the array and assign it to the other.

 int a[] = { 1, 2, 3, 4, 5, 6 };
 int temp[] = new int[a.length];

 for (int i = 0; i < a.length; i++) {
     temp[i] = a[i];
   }

Just remove the "equals".

Change the line:

for (int ixx = 0; ixx <= temp.length; ixx++) {

with this:

for (int ixx = 0; ixx < temp.length; ixx++) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM