Java Class Arrays: ArrayTest Method

In summary, the conversation discusses the creation of a Java class named ArrayTest with a method called arrayCombiner. This method creates three arrays of size 10, with the first containing odd numbers from 1 to 19, the second containing even numbers from 2 to 20, and the third containing the sum of the numbers from the first two arrays. The conversation also includes loops to populate the third array and print out its contents.
  • #1
glebovg
164
1
Homework Statement

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.

The attempt at a solution

Code:
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
 
Physics news on Phys.org
  • #2


I got it.

Code:
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
 

1. What is the purpose of the ArrayTest method in Java Class Arrays?

The ArrayTest method is used to test and manipulate arrays in Java. It allows you to create, modify, and access elements in an array, as well as perform operations such as sorting and searching.

2. How do you declare an array using the ArrayTest method?

To declare an array in Java using the ArrayTest method, you can use the following syntax: datatype[] arrayName = new datatype[length]; For example, to create an array of integers with a length of 5, you would use int[] numbers = new int[5];

3. Can you pass an array as a parameter to the ArrayTest method?

Yes, you can pass an array as a parameter to the ArrayTest method. This allows you to manipulate the contents of the array within the method and have those changes reflected in the original array.

4. What is the difference between ArrayTest and ArrayList in Java?

ArrayTest and ArrayList are both used to store and manipulate a collection of elements in Java, but they have some key differences. ArrayTest is a fixed-size data structure, meaning its length cannot be changed once it is initialized. ArrayList, on the other hand, can dynamically grow or shrink in size. Additionally, ArrayList allows you to store objects of any type, while ArrayTest can only hold elements of the same data type.

5. How do you access individual elements in an array using the ArrayTest method?

You can access individual elements in an array by using the index notation. The first element in an array has an index of 0, the second element has an index of 1, and so on. For example, if you want to access the third element in an array called numbers, you would use numbers[2].

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
950
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top