From cfab36537b7f5ce9a412d9b947fb009de59deeab Mon Sep 17 00:00:00 2001 From: Aalaizha B Date: Thu, 11 May 2023 16:41:04 -0400 Subject: [PATCH] Complete: Array activities --- src/com/example/main/Main.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/com/example/main/Main.java b/src/com/example/main/Main.java index c85b13c..e54d8bc 100644 --- a/src/com/example/main/Main.java +++ b/src/com/example/main/Main.java @@ -1,5 +1,6 @@ package com.example.main; +import java.util.ArrayList; import java.util.Arrays; class Main { @@ -13,21 +14,48 @@ public static void main(String[] args) { //create an array (not an ArrayList) called favoriteColors that holds five strings with your favoriteColors and print the entire array to the screen. + String[] favoriteColors = {"Blue", "Red", "Yellow", "Green", "Sky Blue"}; + System.out.println(Arrays.toString(favoriteColors)); + //Change the third element of favoriteColors to a different color and print that element to the screen + favoriteColors[2] = "Light Yellow"; + System.out.println((favoriteColors[2])); + //Declare and initialize and array of type int called numbers to hold 1000 elements (this should be used later in the loop exercise) + int[] numbers = new int[1000]; + //ArrayList //Declare and initialize an ArrayList so that it holds values of type . + ArrayList doubleArr = new ArrayList(); + //Using the .add() method, add 5 decimal values to the ArrayList and print it to the screen + doubleArr.add(.1); + doubleArr.add(.2); + doubleArr.add(.3); + doubleArr.add(.4); + doubleArr.add(.5); + + System.out.println(doubleArr); + //Using the .remove() method, remove the 3rd value in the ArrayList. + doubleArr.remove(2); + //Print ArrayList to the screen. + System.out.println(doubleArr); + + //Now change the last element in the ArrayList and print the new element to the screen + doubleArr.set(doubleArr.size() - 1, 1.56); + System.out.println(doubleArr); + + /* Some activities for Loops