JavaRush /Java Blog /Random EN /Coffee break #164. Three ways to convert an array to a li...

Coffee break #164. Three ways to convert an array to a list. How to Write a Palindrome Checker in Java

Published in the Random EN group

Three ways to convert an array to a list

Source: Rrtutors Java developers often need to convert arrays to lists, especially when working with lists of elements. In this post, you will learn three ways to convert an array to a list in Java. Coffee break #164.  Three ways to convert an array to a list.  How to write a program to check palindromes in Java - 1

Three ways to convert an array to a list

You can use three methods to convert an array to a list:
  1. Create an empty list and add elements.
  2. Using the Arrays.asList() method .
  3. Using new ArrayList<>(Arrays.asList(arr)) .

Method 1: Create an empty list and then add elements

This method is the simplest of the three, and most developers consider it very trivial and obvious. To use it, you need to follow three simple steps:
  • Step 1: First of all, create an empty list.
  • Step 2: Then iterate through the element array.
  • Step 3: Finally, add these items to your list.
Let's see how these three steps are implemented in practice. In the example below, we are going to create an empty list and add elements.
import java.util.ArrayList;

import java.util.List;

public class Create_List_add_arrays {

            public static void main(String[] args) {

                        String[] OurArray = new String[] { "123", "456", "789" };

        List<String> ourList = new ArrayList<>();

        for (int i=0; i<OurArray.length; i++){

            ourList.add(OurArray[i]);

        }

        System.out.println (ourList);

            }

}
Conclusion:
[123, 456, 789]

Method 2: Using Arrays.asList() Method

Here we will use Arrays.asList(arr) which is a built-in method provided by Arrays to convert an array to a list. Let's look at an example of how this method is implemented:
package Using_Arrays;

import java.util.Arrays;

import java.util.List;

public class using_method {

            public static void main(String[] args) {

                        String[] OurArray = new String[] { "100", "200", "300" };

                    List<String> OurList = Arrays.asList(OurArray);

                    System.out.println(OurList);



            }

}
Conclusion:
[100, 200, 300]
This method works by creating a fixed-size list, which means you won't be able to add more items to it.

Method 3: Using new ArrayList<>(Arrays.asList(arr))

Here we simply use new arrayList<>(Arrays.asList(integers)); to convert our array to a list. The main advantage of this method over the previous one is that the created array allows the user to add more elements to the list. Let's look at the sample code:
import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

public class ArrayList_methods {

            public static void main(String[] args) {

                        String[] ourArray = new String[] { "100", "200", "300" };

        List<String> ourList = new ArrayList<>(Arrays.asList(ourArray));

        System.out.println("Our Array: " + ourList);

        ourList.add("400");

        System.out.println("We add a new element: " + ourList );

            }

}
Conclusion:
Our array: [100, 200, 300] Add a new element: [100, 200, 300, 400]

How to Write a Palindrome Checker in Java

Source: DZone In this article, you will learn two ways to write a palindrome checking program in Java. Coffee break #164.  Three ways to convert an array to a list.  How to Write a Program to Check Palindromes in Java - 2During a Java interview, you may be asked to write a program to check palindromes. This is one of the difficult questions, but quite common during interviews. Palindromes are numbers, words, or strings that read the same in both directions. In turn, their opposite is non-palindromes.

What do palindromes look like?

When letters or numbers are arranged so that they form mirror images of each other, it creates a palindrome. Among the many examples of palindromes are 686, 140041, 95359, 7007, radar, hut, grandfather, and so on. That is, if you read the letters backwards, it becomes obvious that they form mirror images of each other. With the remainder and division operators in Java, we can create code that checks whether a particular number is a palindrome or not.

Stages of creating a program for checking numeric palindromes

  1. We enter or initialize the number to be checked.
  2. We create a temporary variable and store the number in it.
  3. Let's invert the number.
  4. We compare the temporary number with the inverse number.
  5. If both numbers are equal, then it is a palindrome.

Java palindrome program using FOR loop

Below is an easy to use program that uses For Loop to find a palindrome. In a for loop, the digits in the input are checked repeatedly until the input value is 0. The for loop takes into account the modulus of the number (num), which is stored in a variable called reverseNum for each iteration of the loop. As a result, we can get the complete opposite/reverse of the input. The flipped number is then compared to the original number to determine whether it is a palindrome. Here is an example that allows you to check if the number being tested is a palindrome.

Program operation algorithm

  1. Start of the program.
  2. Accept user input or initialize it manually (number).
  3. Create a new variable ( initialNum ) and save the input.
  4. Until num becomes equal to zero, we find the remainder of num and store it in a variable ( reverseNum ).
  5. We determine whether initialNum matches reverseNum .
  6. If both numbers are equal, we conclude that this is a palindrome.
  7. Otherwise it is not a palindrome.
  8. Termination of the program.

Code snippet:

import java.util.*;
class Main
{
  public static void main(String[] args)
    {
        Scanner sc= new Scanner(System.in);
        System.out.print("Enter the number: ");
        int num= sc.nextInt();

        int reverseNum=0, initialNum, remainder=0;
        initialNum = num;
        for(;num!=0;num/=10)
        {
            remainder= num % 10;
            reverseNum = (reverseNum * 10) + remainder;
        }

        if (initialNum == reverseNum)
        {
            System.out.println("Yes, the given number " + initialNum + " is a palindrome.");
        }

        else
        {
           System.out.println("No, the given number " + initialNum + " is not a palindrome.");
        }
    }
}

Conclusion 1

Enter the number: 45354 Yes, the given number 45354 is a palindrome.

Conclusion 2

Enter the number: 61214 No, the given number 61214 is not a palindrome.

Palindrome Program in Java Using While Loop

Having understood the logic of the code, let's now look at another way to write a palindrome program in Java - using a while loop. In a while loop, the digits in the input are checked repeatedly until the input value is 0. The while loop takes into account the modulus of the number (num), which is stored in a variable called reverseNum for each iteration of the loop. Finally, the inverted number is compared to the original number to determine whether it is a palindrome. Here's an example that allows you to check if an input is a palindrome number.

Program operation algorithm

  1. Start of the program.
  2. Accept user input or initialize it manually (number).
  3. Create a new variable ( initialNum ) and save the input.
  4. Until num becomes equal to zero, we find the remainder of num and store it in a variable ( reverseNum ).
  5. We determine whether initialNum is equal to reverseNum .
  6. If both are equal, we deduce that it is a palindrome.
  7. Otherwise it is not a palindrome.
  8. End of the program.

Code snippet

import java.util.*;
class Main
{
    public static void main(String[] args)
    {
        Scanner sc= new Scanner(System.in);
        System.out.print("Enter the number: ");
        int num= sc.nextInt();
        int reverseNum=0, initialNum, remainder;
        initialNum = num;
        while(num!=0)
        {
            remainder= num % 10;
            reverseNum = (reverseNum * 10) + remainder;
            num = num / 10;
        }

        if (initialNum == reverseNum)
        {
            System.out.println("Yes, the given number " + initialNum + " is a palindrome.");
        }
        else
        {
           System.out.println("No, the given number " + initialNum + " is not a palindrome.");
        }
  }
}

Conclusion 1

Enter the number: 98989 Yes, the given number 98989 is a palindrome.

Conclusion 2

Enter the number: 3624251 No, the given number 3624251 is not a palindrome.

Conclusion

In this post, you learned what a palindrome is and how to write code to check palindromes in Java.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION