Answers for "how to add elements in array in java using for loop"

1

how to add elements in array in java using for loop

int[] nums = new int[5];
for(int i = 0; i < nums.length; i++){
  nums[i] = i + 2;
  System.out.println(nums[i]);
}
/*
  OUTPUT:
  2 3 4 5 6
  each time i is increased by 2
*/
Posted by: Guest on June-17-2021
3

java array add element

// A better solution would be to use an ArrayList which can grow as you need it. 
// The method ArrayList.toArray( T[] a ) 
// gives you back your array if you need it in this form.

List<String> where = new ArrayList<String>();
where.add(element);
where.add(element);

// If you need to convert it to a simple array...

String[] simpleArray = new String[ where.size() ];
where.toArray( simpleArray );
Posted by: Guest on September-30-2020
5

how to add objects in array java

car redCar = new Car("Red");
car Garage [] = new Car [100];
Garage[0] = redCar;
Posted by: Guest on February-13-2020

Code answers related to "how to add elements in array in java using for loop"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language