Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Array

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array

    Create a Java class named ArrayTest with a method called arrayCombiner that creates three arrays of size 10. The first array should contain the values 1, 3, 5, 7, 9, 11, 13, 15, 17, and 19. The second array should contain the values 2, 4, 6, 8, 10, 12, 14, 16, 18, and 20. Using loops, populate the third array with the sum of the value of the number at the same index location from the other two arrays. For example, the first element will be 3 (1+2), the second will be 7 (3 + 4) etc. Write another loop that loops through the third array and prints out its contents.

    public class ArrayTest
    {
    public static void main(String[] args)
    {
    arrayCombiner();
    } // end main
    public static void arrayCombiner()
    {
    int[] odd=new int[10];
    int[] even=new int[10];
    int[] sum=new int[10];
    int count=0;
    int i=1;
    while(count<10)
    {
    odd[count]=i;
    even[count]=i+1;
    sum[count]=i+(i+1);
    count++;
    for(int sumCount:sum)
    {
    System.out.printf("%d ",sumCount);
    }
     }
      } // end arrayCombiner
       } // end class ArrayTest


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array

    I got it.

    public class ArrayTest
    {
    	public static void main(String[] args)
    	{
    		arrayCombiner();
    	} // end main
    	public static void arrayCombiner()
    {
    	int[] odd=new int[10]; // create strings of length 10
    	int[] even=new int[10];
    	int[] sum=new int[10];
    	int counter=0; // initialize counter
    	int i=1; // initialize variable
    	while(counter<10)
    {
    	odd[counter]=i; // assign new content to strings
    	even[counter]=i+1;
    	sum[counter]=odd[counter]+even[counter];
    	counter++;
    	i+=2;
    }
    	for(int total:sum)
    	{
    	System.out.printf("%d ",total);
    	}
    } // end arrayCombiner
     
    } // end class ArrayTest

Similar Threads

  1. Array List of Array Lists working for first item but not for second.
    By javapenguin in forum Collections and Generics
    Replies: 6
    Last Post: February 15th, 2012, 05:12 PM
  2. Replies: 4
    Last Post: November 14th, 2011, 10:00 PM
  3. Doubling The Array Size And Randomizing Array Return
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 27th, 2011, 11:50 AM
  4. Replies: 2
    Last Post: May 13th, 2011, 04:08 AM
  5. Replies: 2
    Last Post: May 6th, 2011, 06:19 PM