Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners How can you add elements of two arrayLists in java?
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


mani azadi
How can you add elements of two arrayLists in java?
1 Answer(s)      3 years and 8 months ago
Posted in : Java Beginners


if you have an arrayList of tempArr = [hi, my, name, is, john] and another arrayList of tempArray2 = [a, b, c, d, e] and you want it to result in newArr = [hi a, my b, name c, is d, john e] how do you go about that?


Advertisement
View Answers

April 4, 2011 at 3:04 PM


public class CombineArrays {
 public static void combineValues(int length, int[] array1, int[] array2){      
        int col = 2;
        int k = 1;
        int[][] array = new int[length][col];   


        for( int i = 0; i < length; i++ )
        {
            for( int j = 0; j < 1; j++ )
            {
                array[i][j] = array1[i];
            }
            array[i][k] = array2[i];
        }

        print( array );
            }

public static void print(int[][]array){
    for (int i=0 ; i < array.length ; i++)
        {     System.out.println();
            for  (int j=0 ; j < array[i].length ; j++){
                System.out.print(array[i][j]+" ");
                  }
        }
}
    public static void main(String[] args)
     {
      int arr1[] = {7,7,3};
      int arr2[] = {4,6,2};
      combineValues(3,arr1,arr2);
     }
    }


Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com