como crear un array en java
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
// Declarando un array literal
como crear un array en java
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
// Declarando un array literal
array in 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]);
java array
// ! 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]; }
array in java
Size is fixed
It supports both primitives and objects
Can be also multi-dimensional
arrays in java
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]);
}
}
}
Java Arrays
public class Test {
public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
System.out.print( x );
System.out.print(",");
}
System.out.print("\n");
String [] names = {"James", "Larry", "Tom", "Lacy"};
for( String name : names ) {
System.out.print( name );
System.out.print(",");
}
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us