Found 34489 Articles for Programming

Creating Java Hello World Program

Krantik Chavan
Updated on 24-Feb-2020 12:42:53

376 Views

Let us look at a simple code that will print the words Hello World.ExampleLive Demopublic class MyFirstJavaProgram {    /* This is my first java program.       * This will print 'Hello World' as the output    */    public static void main(String []args) {       System.out.println("Hello World");       // prints Hello World    } }Let's look at how to save the file, compile, and run the program. Please follow the subsequent steps −Open notepad and add the code as above.Save the file as: MyFirstJavaProgram.java.Open a command prompt window and go to the directory where ... Read More

How to get rows and columns of 2D array in Java?

varun
Updated on 24-Feb-2020 12:16:59

8K+ Views

Following example helps to determine the rows and columns of a two-dimensional array with the use of arrayname.length.ExampleFollowing example helps to determine the upper bound of a two dimensional array with the use of arrayname.length. public class Main {    public static void main(String args[]) {       String[][] data = new String[2][5];       System.out.println("Dimension 1: " + data.length);       System.out.println("Dimension 2: " + data[0].length);    } }OutputThe above code sample will produce the following result. Dimension 1: 2 Dimension 2: 5

How to convert comma seperated java string to an array.

Prabhas
Updated on 24-Feb-2020 12:15:58

5K+ Views

Yes, use String.split() method to do it. See the example below −Examplepublic class Tester {    public static void main(String[] args) {       String text = "This,is,a,comma,seperated,string.";       String[] array = text.split(",");       for(String value:array) {          System.out.print(value + " ");       }    } }OutputThis is a comma seperated string.

How to print a byte array in Java?

seetha
Updated on 24-Feb-2020 11:18:02

15K+ Views

You can simply iterate the byte array and print the byte using System.out.println() method.Examplepublic class Tester {    public static void main(String[] args) {       byte[] a = { 1,2,3};       for(int i=0; i< a.length ; i++) {          System.out.print(a[i] +" ");       }    } }Output1 2 3

how to shuffle a 2D array in java correctly?

vanithasree
Updated on 24-Feb-2020 11:17:13

1K+ 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

how to initialize a dynamic array in java?

radhakrishna
Updated on 24-Feb-2020 11:16:20

459 Views

Following program shows how to initialize an array declared earlier.Examplepublic class Tester {    int a[];    public static void main(String[] args) {       Tester tester = new Tester();       tester.initialize();    }    private void initialize() {       a = new int[3];       a[0] = 0;       a[1] = 1;       a[2] = 2;       for(int i=0; i< a.length ; i++) {          System.out.print(a[i] +" ");       }    } }Output0 1 2

How to concatenate byte array in java?

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

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

How to create a dynamic 2D array in Java?

Giri Raju
Updated on 24-Feb-2020 11:15:24

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

How to remove an element from an array in Java

Sreemaha
Updated on 24-Feb-2020 11:11:49

860 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]

How to convert an object x to a string representation in Python?

Malhar Lathkar
Updated on 24-Feb-2020 09:54:15

205 Views

Most commonly used str() function from Python library returns a string representation of object.>>> no=100 >>> str(no) '100' >>> L1=[1,2,3,4] >>> str(L1) '[1, 2, 3, 4]' >>> d={'a': 1, 'b': 2, 'c': 3, 'd': 4} >>> str(d) "{'a': 1, 'b': 2, 'c': 3, 'd': 4}"However, repr() returns a default and unambiguous representation of the object, where as str() gives an informal representation that may be readable but may not be always unambiguous.>>> str(d) "{'a': 1, 'b': 2, 'c': 3, 'd': 4}" >>> repr(d) "{'a': 1, 'b': 2, 'c': 3, 'd': 4}" >>> repr(L1) '[1, 2, 3, 4]' >>> repr(no) '100'

Advertisements