Found 9150 Articles for Object Oriented Programming

How to create an array of Object in Java

Johar Ali
Updated on 07-Mar-2025 17:39:38

847 Views

In this article, we will learn to create an array of objects in Java. An array of Object classes can be created that can accept any type of object. During the operation on such an array, instanceof operator can be used. Different Approaches The following are the two different approaches to creating an array of objects in Java − Using an Object[] Array Using a Class-Specific Object Array Using an Object[] Array Java provides a built-in Object class, which is the superclass of all classes. This means an array ... Read More

How to check whether element is in the array in Java?

Ali
Ali
Updated on 24-Feb-2020 10:55:45

139 Views

Following example uses Contains method to search a String in the Array.Exampleimport java.util.ArrayList; public class Main {    public static void main(String[] args) {    ArrayList objArray = new ArrayList();    ArrayList objArray2 = new ArrayList();    objArray2.add(0, "common1"); objArray2.add(1, "common2");   objArray2.add(2, "notcommon"); objArray2.add(3, "notcommon1");    objArray.add(0, "common1"); objArray.add(1, "common2");    System.out.println("Array elements of array1"+objArray);    System.out.println("Array elements of array2"+objArray2);    System.out.println("Array 1 contains String common2?? " +objArray.contains("common1"));    System.out.println("Array 2 contains Array1?? " +objArray2.contains(objArray) );     }  }OutputThe above code sample will produce the following result.Array elements of array1[common1, common2] Array elements of array2[common1, common2, notcommon, notcommon1] Array ... Read More

How do you write a method in Java that can print object in array?

Every ; Thing
Updated on 05-Mar-2020 11:21:10

160 Views

Exampleclass Shape{     private String shapeName;     private int numSides;      Shape(String shapeName, int numSides){         this.shapeName = shapeName;         this.numSides = numSides;     }     public String toString(){         return shapeName + " has " + numSides + " sides.";     } } class ObjectList{     private Object[] list = new Object[10];     private int numElement = 0;      public void add(Object next){         list[numElement] = next;         numElement++;     }      @Override     public String toString(){         String str="";         int i=0;         while(list[i] != null){             str += list[i]+"";             i++;         }         return str;     } } public class Driver{     public static void main(String[] args){         ObjectList list = new ObjectList();         Shape square = new Shape("square", 4);         Shape hex = new Shape("hexagon", 6);         list.add(hex);         list.add(square);         System.out.println(list);     } }

How to filter an array in Java

Ali
Ali
Updated on 17-Jun-2020 11:30:58

7K+ Views

You can use List.removeAll() method to filter an array. exampleimport java.util.ArrayList; import java.util.List; public class Tester {    public static void main(String[] args) {       List list = new ArrayList();       list.add("A");       list.add("B");       list.add("C");       list.add("D");       list.add("E");       list.add("F");       list.add("G");       list.add("H");       List filters = new ArrayList();       filters.add("D");       filters.add("E");       filters.add("F");       System.out.println("Original List " + list);       list.removeAll(filters);       System.out.println("Filtered List ... Read More

How to convert a byte array to hex string in Java?

Rahul Sharma
Updated on 30-Jul-2019 22:30:21

729 Views

Use Integer.toString(int, redix) where int is byte to be converted and redix is 16 for hexadecimal format.

Find the dimensions of 2D array in Java

Rahul Sharma
Updated on 24-Feb-2020 10:44:41

490 Views

Following example helps to determine the upper bound of a two dimensional array with the use of arrayname.length.https://www.tutorialspoint.com/javaexamples/arrays_upperbound.htm

How to declare a static String array in Java

Alshifa Hasnain
Updated on 20-Mar-2025 18:18:57

1K+ Views

In this article, we will learn the declaration of static Strings array in Java. Arrays can be used to store multiple values in one variable. Static array has a specific size which cannot be increased in the later part of the program after creation. What is a Static String Array? A static array is a declared array as static, which means that it is associated with the class, not with a class instance. This brings the array into a shared state among all instances of the class. Static variables are loaded when the class is loaded, even before class instances ... Read More

Returning an Array from a Method in Java

Giri Raju
Updated on 25-Feb-2020 05:21:22

278 Views

A method may also return an array. For example, the following method returns an array that is the reversal of another array -Examplepublic static int[] reverse(int[] list) {    int[] result = new int[list.length];    for (int i = 0, j = result.length - 1; i < list.length; i++, j--) {       result[j] = list[i];    }    return result; }

Passing array to method in Java

Jai Janardhan
Updated on 25-Feb-2020 05:05:56

375 Views

Just as you can pass primitive type values to methods, you can also pass arrays to methods. For example, the following method displays the elements in an int array -Examplepublic static void printArray(int[] array) {    for (int i = 0; i < array.length; i++) {       System.out.print(array[i] + " ");    } }You can invoke it by passing an array. For example, the following statement invokes the print Array method to display 3, 1, 2, 6, 4, and 2 -printArray(new int[]{3, 1, 2, 6, 4, 2});

public access modifier in Java

Vrundesha Joshi
Updated on 24-Feb-2020 12:32:52

344 Views

A class, method, constructor, interface, etc. declared public can be accessed from any other class. Therefore, fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe.However, if the public class we are trying to access is in a different package, then the public class still needs to be imported. Because of class inheritance, all public methods and variables of a class are inherited by its sub classes.ExampleThe following function uses public access control -public static void main(String[] arguments) {    // ... }The main() method of an application has to be ... Read More

Advertisements