kolunar
0
Q:

array in java

// Accessing array elements using for loop
public class AccessingArrayElements
{
   public static void main(String[] args)
   {
      int[] arrNum = {25, 23, 15, 20, 24};
      for(int a = 0; a < arrNum.length; a++)
      {
         System.out.println(arrNum[a]);
      }
   }
}
3
//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
For example: int[ ] num = new int[6];
public class AccessingArrayElements
{
   public static void main(String[] args)
   {
      int[] arrNum = {25, 23, 15, 20, 24};
      for(int a = 0; a < arrNum.length; a++)
      {
         System.out.println(arrNum[a]);
      }
   }
}
3
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
int[] arr;			// Declaration dataType can int, float etc.
arr = new int[N];	// Allocation N = 0 to 2,147,483,647.
6
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]);
// Now outputs Opel instead of Volvo
7

New to Communities?

Join the community