How to define and use int array in Java

4 Answers

0 votes
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        int[] arr1 = {1, 28, 9, 3, 5};
        int arr2[] = {1, 28, 9, 3, 5};  
        
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));
    }
}



/*
run:

[1, 28, 9, 3, 5]
[1, 28, 9, 3, 5]

*/

 



answered Jul 20, 2020 by avibootz
0 votes
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        int arr[] = new int[]{1, 28, 9, 3, 5};  
        
        System.out.println(Arrays.toString(arr));
    }
}



/*
run:

[1, 28, 9, 3, 5]

*/

 



answered Jul 20, 2020 by avibootz
0 votes
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        int arr[] = new int[5];
        
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }

        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i] );
        }
    }
}



/*
run:

0
1
2
3
4

*/

 



answered Jul 20, 2020 by avibootz
0 votes
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        int arr[] = new int[7];
        
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }

        for (int n : arr) {
            System.out.println(n);
        }
    }
}



/*
run:

0
1
2
3
4
5
6

*/

 



answered Jul 20, 2020 by avibootz

Related questions

1 answer 152 views
1 answer 118 views
2 answers 110 views
110 views asked Jul 20, 2020 by avibootz
1 answer 120 views
2 answers 182 views
2 answers 141 views
1 answer 195 views
...