Q:

array java

//method 1
int[] age = new int[3];
        age[0] = 1;
        age[1] = 3;
        age[2] = 6;

        for (int i=0; i < 3; i++)
            System.out.println(age[i]);

//method 2
        int[] num = {3,3,5};
        //int num[] = {3,3,5}; also works the same

        System.out.println(num[0]);
13
int[] intArray = new int[20];
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
4
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
23
//Metodo 1
type nome = new type[n];

//Metodo 2
type nome[];
nome = new type[n];
3
// ! IMPORTANTE !
// in JAVA an array is not the same as an ArrayList object!!
// 1 - declare, instanciate and populate
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 
// 2 - declare and instanciate an int array with maxSize
// note: the index goes between 0 and maxSize-1
int newarr[] = new int[maxSize];
// 2.1 - insert the value n on the position pos
newarr[pos] = n; 
// 2.2 - insert values recursively
for (i = 0; i < maxSize; i++) { newarr[i] = arr[i]; }
8
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]);
// Now outputs Opel instead of Volvo
7
type nome[] = new type[n];
1
 int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; 
 // Declaring array literal
0

New to Communities?

Join the community