Java Articles

Page 343 of 450

how to shuffle a 2D array in java correctly?

vanithasree
vanithasree
Updated on 24-Feb-2020 2K+ Views

Yes. Create a list to represent a 2D array and then use Collections.shuffle(list).Exampleimport java.util.ArrayList; import java.util.Collections; 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[]{4,5,6}); rows.add(new int[]{7,8,9}); System.out.println("Before Shuffle"); System.out.println("[0][0] : " + rows.get(0)[0]); System.out.println("[1][1] : " + rows.get(1)[1]); System.out.println("After Shuffle"); Collections.shuffle(rows); System.out.println("[0][0] : " + rows.get(0)[0]); System.out.println("[1][1] : " + rows.get(1)[1]); } }OutputBefore Shuffle [0][0] : 1 [1][1] : 5 After Shuffle [0][0] : 7 [1][1] : 2

Read More

How to create a dynamic 2D array in Java?

Giri Raju
Giri Raju
Updated on 24-Feb-2020 3K+ 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}); ...

Read More

How to create a subarray from another array in Java

Kumar Varma
Kumar Varma
Updated on 24-Feb-2020 18K+ 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

Read More

Removal of negative numbers from an array in Java

Rama Giri
Rama Giri
Updated on 24-Feb-2020 855 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 ]

Read More

how can I declare an Object Array in Java?

Arjun Thakur
Arjun Thakur
Updated on 24-Feb-2020 340 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

Read More

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

Arushi
Arushi
Updated on 24-Feb-2020 249 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

Read More

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

Amit Sharma
Amit Sharma
Updated on 24-Feb-2020 3K+ 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")); ...

Read More

How do I reverse an int array in Java

Ali
Ali
Updated on 24-Feb-2020 247 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

Read More

Why is char[] preferred over String for storing passwords?

Sreemaha
Sreemaha
Updated on 24-Feb-2020 243 Views

Yes, Storing password in String object is not safe for following reasons −String objects are immutable and until garbage collected, they remain in memory.String being plain text can be tracked in memory dump of the application.In log, String based password may be printed which can cause a problem.Char[] can be cleared or wiped out after the job is done.

Read More

Converting ArrayList to String[] in java

Fendadis John
Fendadis John
Updated on 24-Feb-2020 521 Views

Following program is converting an ArrayList to String[];Exampleimport java.util.ArrayList; import java.util.List; public class Tester { public static void main(String[] args) { List names = new ArrayList(); names.add("A"); names.add("B"); names.add("C"); String[] nameArray = names.toArray(new String[names.size()]); for(String name: nameArray) { System.out.println(name); } } }OutputA B C

Read More
Showing 3421–3430 of 4,498 articles
« Prev 1 341 342 343 344 345 450 Next »
Advertisements