Found 9321 Articles for Object Oriented Programming

remove null value from a String array in Java

Jai Janardhan
Updated on 30-Jul-2019 22:30:21

3K+ Views

Following program creates an array with null values. Convert it a list with not-null values only and then get the array of that list.Exampleimport java.util.ArrayList; import java.util.List; public class Tester { public static void main(String[] args) { String[] array = {"I", null, "love", null, "Java" }; List values = new ArrayList(); for(String data: array) { if(data != null) { values.add(data); } } String[] target = values.toArray(new String[values.size()]); for(String data: target) { System.out.println(data + " "); } } }OutputI love Java

Returning an array in Java

George John
Updated on 30-Jul-2019 22:30:21

144 Views

Yes, an array can be returned from a java function. See the example below − Example public class Tester { public static void main(String[] args) { int[] array = getData(); for(int i: array) { System.out.println(i); } } public static int[] getData() { int[] dataArray = {1, 2, 3, 4}; return dataArray; } } Output 1 2 3 4

How do you find the sum of all the numbers in a java array

Arushi
Updated on 24-Feb-2020 10:38:11

131 Views

Following program print the sum of the all the numbers in an array.Examplepublic class Tester {    public static void main(String[] args) {       int[] dataArray = {1, 2, 3, 4};       int sum = 0;       for(int i: dataArray) {          sum += i;       }       System.out.println(sum);    } }Output10

Which one is faster Array or List in Java

Moumita
Updated on 30-Jul-2019 22:30:21

736 Views

The array is faster in case of access to an element while List is faster in case of adding/deleting an element from the collection.

When to use LinkedList over ArrayList in Java

Paul Richard
Updated on 30-Jul-2019 22:30:21

2K+ Views

LinkedList should be used where modifications to a collection are frequent like addition/deletion operations. LinkedList is much faster as compare to ArrayList in such cases. In case of read-only collections or collections which are rarely modified, ArrayList is suitable.

How to convert a String to and fro from UTF8 byte array

Amit Sharma
Updated on 24-Feb-2020 10:36:31

2K+ Views

Following example will showcase conversion of a Unicode String to UTF8 byte[] and UTF8 byte[] to Unicode byte[] using Reader and Writer classes.ExampleIOTester.javaimport java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import java.nio.charset.Charset; import java.text.ParseException; public class I18NTester {    public static void main(String[] args) throws ParseException, IOException {    String input = "This is a sample text" ;    InputStream inputStream = new ByteArrayInputStream(input.getBytes());    //get the UTF-8 data Reader    reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));        //convert UTF-8 to Unicode    int data = reader.read();    while(data != ... Read More

How do I reverse an int array in Java

Ali
Ali
Updated on 24-Feb-2020 10:35:31

127 Views

Following program reverses an int array.Examplepublic class Tester {    public static void main(String[] args) {       int[] numbers = {1,2,3,4,5};       //swap the numbers till the midpoint comes       for (int start = 0, end = numbers.length - 1;       start

what is the simplest way to print a java array

Johar Ali
Updated on 24-Feb-2020 10:34:41

80 Views

To make things simple, convert the array to list and then print it.Exampleimport java.util.Arrays; import java.util.List; public class Tester {    public static void main(String[] args) {       Integer[] numbers = {1,2,3,4,5};       List list = Arrays.asList(numbers);       System.out.println(list);    } }Output[1,2,3,4,5]

How to create a subarray from another array in Java

Kumar Varma
Updated on 24-Feb-2020 10:42:51

15K+ Views

Use Arrays.copyOfRange() method to get a subarray.Exampleimport java.util.Arrays; public class Tester {    public static void main(String[] args) {       int[] array = new int[] {1, 2, 3, 4, 5};       int[] subArray = Arrays.copyOfRange(array, 0, 2);       System.out.println("Array: ");       for(int i = 0; i < array.length; i++) {          System.out.print(array[i] + " ");       }       System.out.println("Sub array: ");       for(int i = 0; i < subArray.length; i++) {          System.out.print(subArray[i] + " ");       }    } }OutputArray: 1 2 3 4 5 Sub array: 1 2

Removal of negative numbers from an array in Java

Rama Giri
Updated on 24-Feb-2020 10:41:59

534 Views

Following program shows how to remove negative numbers from an array.Exampleimport java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Tester {    public static void main(String[] args) {       List objArray = new ArrayList();       objArray.clear();       objArray.add(2);       objArray.add(-3);       objArray.add(4);       System.out.println("Array before removing an element "+objArray);       Iterator iterator = objArray.iterator();                while(iterator.hasNext()) {          Integer next = iterator.next();          if(next < 0) {             iterator.remove();          }       }       System.out.println("Array after removing an element"+objArray);    } }OutputArray before removing an element [ 2, -3, 4 ] Array after removing an element [ 2, 4 ]

Advertisements