![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9326 Articles for Object Oriented Programming
![usharani](https://www.tutorialspoint.com/assets/profiles/13564/profile/60_32161-1512638316.jpg)
346 Views
Yes, break statement can be used in nested for loops. It works on the for loop in which it is applied. See the example below.Examplepublic class Tester { public static void main(String[] args) { for(int i = 0; i< 2; i++) { for(int j = 0; j < 2; j++){ if(i == 1) { break; } System.out.println("i = " + i+",j = " + j); } } } }
![Rahul Sharma](https://www.tutorialspoint.com/assets/profiles/14297/profile/60_102977-1513054044.jpg)
4K+ Views
Use List.clear() method to empty 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"); System.out.println("Original List " + list); list.clear(); System.out.println("Cleared List " + list); } }OutputOriginal List [A, B, C, D, E, F, G, H] Cleared List []
![Johar Ali](https://www.tutorialspoint.com/assets/profiles/14293/profile/60_101691-1513053093.jpg)
519 Views
Array of Object class can be created which can accept any type of object. During operation on such array, instanceof operator can be used.Examplepublic class Tester { public static void main(String[] args) { Object[] dataArray = new Object[3]; dataArray[0] = new Integer(0); dataArray[1] = new String("1"); dataArray[2] = new Boolean(false); for(Object data: dataArray){ if(data instanceof Integer){ System.out.println(((Integer) data).intValue()); } if(data instanceof String){ System.out.println(data); } if(data instanceof Boolean){ System.out.println(((Boolean) data).booleanValue()); } } } }Output0 1 false
![Ali](https://www.tutorialspoint.com/assets/profiles/14292/profile/60_101553-1513052445.jpg)
74 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
![Every ; Thing](https://www.tutorialspoint.com/assets/profiles/27891/profile/60_61166-1522731475.jpg)
109 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); } }
![Ali](https://www.tutorialspoint.com/assets/profiles/14292/profile/60_101553-1513052445.jpg)
5K+ 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
![Johar Ali](https://www.tutorialspoint.com/assets/profiles/14293/profile/60_101691-1513053093.jpg)
1K+ Views
Following program shows how to declare a static string array.Examplepublic class Tester { private static String[] array; static { array = new String[2]; array[0] = "Hello"; array[1] = "World"; } public static void main(String[] args) { System.out.println("Array: "); for(int i = 0; i < array.length; i++){ System.out.print(array[i] + " "); } } }OutputArray: Hello World
![Giri Raju](https://www.tutorialspoint.com/assets/profiles/13536/profile/60_72105-1512540773.jpg)
195 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; }