Object Oriented Programming Articles

Page 301 of 589

How to compare two ArrayList for equality in Java?

Srinivas Gorla
Srinivas Gorla
Updated on 11-Mar-2026 14K+ Views

You can compare two array lists using the equals() method of the ArrayList class, this method accepts a list object as a parameter, compares it with the current object, in case of the match it returns true and if not it returns false.Exampleimport java.util.ArrayList; public class ComparingList {    public static void main(String[] args) {       ArrayList list1 = new ArrayList();       list1.add("JavaFx");       list1.add("Java");       list1.add("WebGL");       list1.add("OpenCV");       ArrayList list2 = new ArrayList();       list2.add("JavaFx");       list2.add("Java");       list2.add("WebGL");       list2.add("OpenCV");       System.out.println(list2);       System.out.println(list1.equals(list2));    } }

Read More

How to convert a list collection into a dictionary in Java?

Nikitha N
Nikitha N
Updated on 11-Mar-2026 2K+ Views

Following is an example to convert a list collection into a dictionary in Java.Exampleimport java.util.ArrayList; import java.util.Dictionary; import java.util.Hashtable; public class CollectionDictionary {    public static void main(String[] args) {       ArrayList list = new ArrayList();       list.add("JavaFx");       list.add("Java");       list.add("WebGL");       list.add("OpenCV");       System.out.println(list);       Dictionary dictionary = new Hashtable();       Hashtable hashTable = new Hashtable();       hashTable.put(1, list.get(0));       hashTable.put(2, list.get(1));       hashTable.put(3, list.get(2));       hashTable.put(4, list.get(3));       System.out.println(hashTable);    } }Output[JavaFx, Java, WebGL, OpenCV] {4=OpenCV, 3=WebGL, 2=Java, 1=JavaFx}

Read More

Fibonacci series program in Java without using recursion.

Syed Javed
Syed Javed
Updated on 11-Mar-2026 4K+ Views

Following is the required program.Examplepublic class Tester {    public static void main(String args[]) {        int n1 = 0, n2 = 1, n3, i, max = 5;        System.out.print(n1 + " " + n2);        for (i = 2; i < max; ++i) {           n3 = n1 + n2;           System.out.print(" " + n3);           n1 = n2;           n2 = n3;        }     }  }Output0 1 1 2 3

Read More

Prime number program in Java.

V Jyothi
V Jyothi
Updated on 11-Mar-2026 2K+ Views

Following is the required program.Examplepublic class Tester {    public static void main(String args[]) {       int i, m = 0, flag = 0;       int n = 41;// it is the number to be checked       m = n / 2;       if (n == 0 || n == 1) {          System.out.println(n + " not a prime number");       } else {          for (i = 2; i

Read More

Factorial program in Java without using recursion.

Vikyath Ram
Vikyath Ram
Updated on 11-Mar-2026 2K+ Views

Following is the required program.Examplepublic class Tester {    static int factorial(int n) {       if (n == 0)          return 1;       else          return (n * factorial(n - 1));    }    public static void main(String args[]) {       int i, fact = 1;       int number = 5;       fact = factorial(number);       System.out.println(number + "! = " + fact);    } }Output5! = 120

Read More

How to match a line not containing a word in Java Regex

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 536 Views

Exampleimport java.util.regex.Matcher; import java.util.regex.Pattern; public class NoRegTest {    public static void main(String[] args) {       String s="^fun";       Pattern pattern = Pattern.compile(s);       Matcher matcher = pattern.matcher("Java is fun");       if(!matcher.find()) {          System.out.println("not found");       }    } }Outputnot found

Read More

How to Initialize and Compare Strings in Java?

George John
George John
Updated on 11-Mar-2026 257 Views

You can compare Strings using compareTo() method or, equals() method or, or == operator. Following example demonstrates how to initialize and compare strings in Java. Example public class StringDemo { public static void main(String[] args) { String str1 = "tutorials"; String str2 = "point"; // comparing str1 and str2 int retval = str1.compareTo(str2); // prints the return value of the comparison if (retval < 0) { System.out.println("str1 is greater than str2"); } else if (retval == 0) { System.out.println("str1 is equal to str2"); } else { System.out.println("str1 is less than str2"); } } } Output str1 is less than str2

Read More

How to convert a Java String to an Int?

Alankritha Ammu
Alankritha Ammu
Updated on 11-Mar-2026 417 Views

The parseXxx() method is used to get the primitive data type of a certain String. In this case to convert a String to integer you could use the parseInt() method.Examplepublic class Test {    public static void main(String args[]) {       int x =Integer.parseInt("9");       double c = Double.parseDouble("5");       int b = Integer.parseInt("444",16);       System.out.println(x);       System.out.println(c);       System.out.println(b);    } }Output9 5.0 1092

Read More

Write a Java program to capitalize each word in the string?

Akshaya Akki
Akshaya Akki
Updated on 11-Mar-2026 2K+ Views

You can capitalize words in a string using the toUpperCase() method of the String class. This method converts the contents of the current string to Upper case letters.Examplepublic class StringToUpperCaseEmp {    public static void main(String[] args) {       String str = "string abc touppercase ";       String strUpper = str.toUpperCase();       System.out.println("Original String: " + str);       System.out.println("String changed to upper case: " + strUpper);    } }OutputOriginal String: string abc touppercase String changed to upper case: STRING ABC TOUPPERCASE

Read More

Java Program to check whether one String is a rotation of another.

Ayyan
Ayyan
Updated on 11-Mar-2026 621 Views

To find weather a string is rotation of another, concat the first string to itself twice and find weatherExamplepublic class Sample {    public static void main(String args[]){       String str1 = "gala";       String str2 = "alag";       String s3 = str1+str1;       if(s3.contains(str2)) {          System.out.println("str1 is rotation of str2");       } else {          System.out.println("str1 is not rotation of str2");       }    } }

Read More
Showing 3001–3010 of 5,881 articles
« Prev 1 299 300 301 302 303 589 Next »
Advertisements