Question

In Java: Create an array of Integers with 10 elements, loop (use a for loop) through...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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)

JAVA (HotSpot 8u 112) 1 2 3 7 8 9 11 import java.util.ArrayList; class Example 4-{ 5 public static void main (String[] args)

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

Add a comment
Know the answer?
Add Answer to:
In Java: Create an array of Integers with 10 elements, loop (use a for loop) through...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT