Black_Kitty
Black_Kitty

Reputation: 15

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

Upvotes: 0

Views: 98

Answers (7)

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35587

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]);
    }

Upvotes: 0

SatyaTNV
SatyaTNV

Reputation: 4135

  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

Upvotes: 0

Thomas D. K. Teixeira
Thomas D. K. Teixeira

Reputation: 96

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++) {

Upvotes: 0

Uma Kanth
Uma Kanth

Reputation: 5629

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];
   }

Upvotes: 0

2787184
2787184

Reputation: 3901

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));

Upvotes: 0

user5118384
user5118384

Reputation:

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]);
}

Upvotes: 2

Ankur Singhal
Ankur Singhal

Reputation: 26077

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

Upvotes: 0

Related Questions