0

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

6
  • 2
    I am not sure what are you trying to do here? Do you want to put all elements in ages[] to temp[]? Commented Jul 17, 2015 at 11:32
  • What do you want to do with this code? Combining two arrays or just reading one index by index? I don't really understand your code :D Commented Jul 17, 2015 at 11:34
  • yes i wanted to put all elements in ages [] to temp[] Commented Jul 17, 2015 at 11:36
  • Change ixx <= temp.length to ixx < temp.length. There it jumps out of bounds. No clue why that loop is even there, because you have 2 loops with the same condition. Commented Jul 17, 2015 at 11:36
  • @ignarapacon oki. Then you can follow my answer. Commented Jul 17, 2015 at 11:50

7 Answers 7

2

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]); } 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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 

Comments

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

Comments

0

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

Comments

0

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

Comments

0
 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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.