In Java:
Create an array of Integers with 10 elements, loop (use a for loop) through the array of integers printing their values to the screen.
Create an array of Strings with 10 elements, loop (use a for loop) through the array of Strings printing their values to the screen.
Finally, create an ArrayList of Integers. Use the loop from part 1 above and add each integer as you print it to the ArrayList as well, then start a final loop (using the for each nomenclature) to print out the list of Integers.
Below is the complete and running Java code (well commented, indented for your understanding ) containing all the three implementations. Also attached are the sample output and the code screenshots.
import java.util.ArrayList;
class Example
{
public static void main (String[] args) throws java.lang.Exception
{
//declare array of size 10 of type integer
//and initialize the array
int [] array = new int[] {1,2,3,4,5,6,7,8,9,10};
System.out.println("Integer Array");
for(int i = 0; i<array.length ; i++) { //loop through the array
System.out.println("Element at index " + i + " = "+ array[i]);
}
//declare string array of size 10
String[] str_array = new String[] {"This","is","an","example","java","program","for",
"learning","arrays","."};
System.out.println("\nString Array");
for(int i = 0; i<str_array.length ; i++) {
System.out.println("Element at index " + i + " = "+ str_array[i]);
}
ArrayList<Integer> array_list = new ArrayList<Integer>(); //creates an empty array list
for(int i = 0; i<array.length ; i++) {
array_list.add(array[i]); //add element at index i in array to array_list
}
//print the arraylist
for (int i : array_list) {
System.out.println(i);
}
}
}
Output and screenshots:
Integer Array
Element at index 0 = 1
Element at index 1 = 2
Element at index 2 = 3
Element at index 3 = 4
Element at index 4 = 5
Element at index 5 = 6
Element at index 6 = 7
Element at index 7 = 8
Element at index 8 = 9
Element at index 9 = 10
String Array
Element at index 0 = This
Element at index 1 = is
Element at index 2 = an
Element at index 3 = example
Element at index 4 = java
Element at index 5 = program
Element at index 6 = for
Element at index 7 = learning
Element at index 8 = arrays
Element at index 9 = .
1
2
3
4
5
6
7
8
9
10
Screenshots: (to refer indentation)
Please do let me know for sure if you have any kind of doubts/questions regarding the solution or anything, I would be happy to help you. Thanks
In Java: Create an array of Integers with 10 elements, loop (use a for loop) through...
In java, request and read in 10 integers from the terminal and put into an ArrayList. Remember, an ArrayList can only store object references, so you will need to take advantage of autoboxing. Then add the numbers together that have been placed in the array, first with the use of an iterator, and then with an enhanced for loop, printing out the results in each case.
Write a Java program that will create an array of Strings with 25 elements. Input Strings from the user and store them in the array until either the user enters “quit” or the array is full. HINT: the sentinel value is “quit” all lowercase. “Quit” or “QUIT” should not stop the loop. HINT: you have to use the equals method (not ==) when you are comparing two Strings. After the input is complete, print the Strings that the user entered...
write a java program Accept a positive integer n from keyboard and then create an array or arraylist containing n random elements within the range [-n,n). Print out the random array or arraylist, and then find out and print out the number of inversions and the maximum subarray (index range of the maximum subarray along with the maximum subarray sum) in the array or arraylist using divide and conquer, respectively. For example, suppose we accept integer 6 from keyboard, then...
In Java create an integer array named dice1 with a size of 10. Populate each array location with a roll of a six-sided die (hint: an int value of 1 through 6). Print the array out using an enhanced for loop. Sample output: Question 1 dice1 = 1 1 6 2 3 5 1 5 4 5
Write a program that creates an array of 10 integers. The array should be populated with values: each element should be equal to its subscript/index. The program should then print each element. Make use of a loop in storing values to the array. Make use of another loop in printing the values of the array.
In Java: Executable Class create an array of Employee objects. You can copy the array you made for Chapter 20. create an ArrayList of Employee objects from that array. use an enhanced for loop to print all employees as shown in the sample output. create a TreeMap that uses Strings for keys and Employees as values. this TreeMap should map Employee ID numbers to their associated Employees. process the ArrayList to add elements to this map. print all employees in...
Must use JOPTIONPANE. Using arrays and methods create a JAVA program. Create a java program that holds an array for the integer values 1-10. Pass the array to a method that will calculate the values to the power of 2 of each element in the array. Then return the list of calculated values back to the main method and print the values
Please write a Java program that does the following: Create an array of 100 integers. Store 100 random integers (between 1 and 100) in the array. Print out the elements of the array. Sort the array in ascending order. Print out the sorted array. Prompt the user to enter a number between 1 and 100. Search the array for that number and then display "Found" or "Not Found" message. Display each number from 1 to 100 and the number of...
Write a Java program that allows the following: 1.Create an array list of 20 integers. The 20 integers are generated randomly with possible values from 0 to 10. 2.Print the content of the array list. 3.Find how many of these values are multiple of 3. Show a screenshot of the output with your name and Id in the first line (if there is no screenshot of the output, the student shall get a zero mark for this question). Program typical...
In JAVA write a program that contains an array containing 10 integers (1 to 10). user insert an integer from (1 to 10) , method is library sort or gapped insertion sort that will each sort the array and print it out to the console.