- 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
2K+ Views
You ByteArrayOutputStream to write byte arrays and get the result using its toByteArray() method.import java.io.ByteArrayOutputStream; import java.io.IOException; public class Tester { public static void main(String[] args) throws IOException { byte[] a = { 1,2,3}; byte[] b = { 4,5,6}; ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write(a); baos.write(b); byte[] c = baos.toByteArray(); for(int i=0; i< c.length ; i++){ System.out.print(c[i] +" "); } } }Output1 2 3 4 5 6
2K+ Views
If you wish to create a dynamic 2d array in Java without using List. And only create a dynamic 2d array in Java with normal array then click the below linkYou can achieve the same using List. See the below program. You can have any number of rows or columns.Exampleimport java.util.ArrayList; import java.util.List; public class Tester { public static void main(String[] args) { List rows = new ArrayList(); rows.add(new int[]{1, 2, 3}); rows.add(new int[]{1, 2}); rows.add(new int[]{1}); //get element at row : 0, column ... Read More
844 Views
Following example shows how to remove an element from array. Exampleimport java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList objArray = new ArrayList(); objArray.clear(); objArray.add(0,"0th element"); objArray.add(1,"1st element"); objArray.add(2,"2nd element"); System.out.println("Array before removing an element"+objArray); objArray.remove(1); objArray.remove("0th element"); System.out.println("Array after removing an element"+objArray); } }OutputThe above code sample will produce the following result. Array before removing an element[0th element, 1st element, 2nd element] Array after removing an element[2nd element]
2K+ Views
You can either setLength to be 0 or create a new StringBuilder() instance. See the example below −Examplepublic class Tester { public static void main(String[] args) { StringBuilder builder = new StringBuilder(); builder.append("sample"); System.out.println(builder.toString()); builder.setLength(0); System.out.println(builder.toString()); builder.append("sample"); System.out.println(builder.toString()); } }Outputsample sample
687 Views
You can simply iterate through list and fill the array as shown below −import java.util.ArrayList; import java.util.List; public class Tester { public static void main(String[] args) { List list = new ArrayList(); list.add(new Integer(1)); list.add(new Integer(2)); list.add(new Integer(3)); list.add(new Integer(4)); int[] array = new int[list.size()]; for(int i=0;i
4K+ Views
Yes. From Java 8 onwards, we can do so using method references.Method references help to point to methods by their names. A method reference is described using "::" symbol. A method reference can be used to point the following types of methods −Static methodsInstance methodsConstructors using new operator (TreeSet::new)Method Reference ExampleCreate the following Java program using any editor of your choice in, say, C:\> JAVA.Java8Tester.java Live Demo import java.util.List; import java.util.ArrayList; public class Java8Tester { public static void main(String args[]) { List names = new ArrayList(); names.add("Mahesh"); names.add("Suresh"); names.add("Ramesh"); ... Read More
616 Views
A method can give multiple values if we pass an object to the method and then modifies its values. See the example below −Examplepublic class Tester { public static void main(String[] args) { Model model = new Model(); model.data1 = 1; model.data2 = 2; System.out.println(model.data1 + ", " + model.data2); changeValues(model); System.out.println(model.data1 + ", " + model.data2); } public static void changeValues(Model model) { model.data1 = 100; model.data2 = 200; } } class Model { int data1; int data2; }Output1, 2 100, 200
607 Views
Call by Value means calling a method with a parameter as value. Through this, the argument value is passed to the parameter.While Call by Reference means calling a method with a parameter as reference. Through this, the argument reference is passed to the parameter.In call by value, the modification done to the parameter passed does not reflect in the caller's scope while in call by reference, the the modification done to the parameter passed are persistent and changes are reflected in the caller's scope.But Java uses only call by value. It creates a copy of references and pass them as ... Read More