Saturday, May 28, 2011

TwoArrays in Java

 Two Arrays in Java:Sample program
        public class TwoArrays {

       public static void main(String[] args) {
       int val[] = {13,-4,82,17};
       int twice[] = new int [4];
       System.out.println("Original Array: "+ val[0]+","+val[1]+","+val[2]+","+val[3]);

       //the next line of coude would doulbe the values of val and assign them to twice


        twice[0] = val[0]*2;
       twice[1] = val[1]*2;
       twice[2] = val[2]*2;
       twice[3] = val[3]*2;

      System.out.println("New Array: "+ twice[0]+","+twice[1]+","+twice[2]+","+twice[3]);

//another way to do it
        twice[0] = val[0]+val[0];
       twice[1] = val[1]+
val[1];
       twice[2] = val[2]+
val[2];
       twice[3] = val[3]+
val[3];
System.out.println("New Array: "+ twice[0]+","+twice[1]+","+twice[2]+","+twice[3]);
    }

}

No comments: