Java Program To Square All Items In List

Chapter: Miscellaneous Last Updated: 17-05-2023 15:12:46 UTC


            /* ............... START ............... */
                import java.util.ArrayList;
import java.util.List;

public class SquareListItemsExample {
    public static void main(String[] args) {
        // Create a list
        List<Integer> numbers = new ArrayList<>();

        // Square all items in the list
        for (int i = 0; i < numbers.size(); i++) {
            int squared = numbers.get(i) * numbers.get(i);
            numbers.set(i, squared);

        // Print the squared list
        System.out.println("Squared list: " + numbers);

                /* ............... END ............... */


Squared list: [1, 4, 9, 16, 25]


  • In this example, we create an ArrayList called numbers and add some integer values to it.
  • To square all items in the list, we use a for loop to iterate over each element. For each element, we retrieve its value using numbers.get(i), square it by multiplying it with itself, and store the squared value in a variable called squared. Then, we use the numbers.set(i, squared) method to update the element in the list with its squared value.
  • After squaring all items in the list, we print the squared list using System.out.println(), which displays the squared list as [1, 4, 9, 16, 25].
  • So, the program demonstrates how to square all items in a list by iterating over each element and updating it with its squared value in Java.


Java Program To Square All Items In List #How do you square a list in Java? #Square values in array list Java

Similar Programs Chapter Last Updated
Swap Two Numbers Without Using Third Variable In Java Miscellaneous 02-06-2023
Java Program To Find The Average Of An Array Of Numbers Miscellaneous 02-06-2023
How Do You Find The Factorial Of A Number In Java Miscellaneous 02-06-2023
Java Program That Takes Two Numbers As Input And Prints Their Sum Miscellaneous 27-05-2023
How To Get The Length Of An Array In Java Miscellaneous 27-05-2023
Java Add Element To List Example Miscellaneous 19-05-2023
Java Program To Merge Two Lists Miscellaneous 17-05-2023
How To Reverse A List In Java Miscellaneous 17-05-2023
Java Program To Find Unique Elements In An Array Miscellaneous 14-05-2023
Java Program To List All Elements In List Miscellaneous 30-04-2023
Java Program To Create XML File Miscellaneous 23-04-2023
Java Program To Run Something Every X Minutes Miscellaneous 22-04-2023
Java Program To Reverse A String Miscellaneous 22-04-2023
Java Program To Implement Method Overloading Miscellaneous 16-04-2023
Java Parse JSON String To Object Miscellaneous 24-03-2023
Java Program For Date And Time Miscellaneous 24-03-2023
Java Binary Search Tree Implementation Miscellaneous 21-03-2023
Java Program To Merge Two PDF Files Miscellaneous 18-03-2023
How To Create A Git Repository | Git repository commands Miscellaneous 31-07-2021
Currency Formatter In Java | How To Format Currency In Java Miscellaneous 19-07-2021
Factory Design Pattern In Java Miscellaneous 11-05-2021
Data Types In Java Miscellaneous 09-06-2018