Extract elements of array / extract in reverse order-java

sample


		int[] array = new int[5];
		//1 as an element in the declared array,10,100,1000,Set 10000

		array[0] = 1;    //Note that the array starts at 0th.
		array[1] = 10;
		array[2] = 100;
		array[3] = 1000;
		array[4] = 10000; 
		
		System.out.println(array[0]); //Print the first value in the array

Method using array declaration {}. Outputs the sum of the 1st and 3rd of the array

sample


		int[] array = {1,10,100};

		System.out.println(array[0]+array[2]);




		for(int i=0; i<array.length; i++) {
			System.out.println(array[i]);
		}//Output all elements of array array in order


		for(int i=array.length-1; i>=0; i--) {
			System.out.println(array[i]);
		}//Output the contents of the array array in order from the end to the beginning(Take out from the reverse)

		//Number of arrays in int i when outputting from the end-Set 1 as the initial value

When outputting from the end For example, if the array array has three lengths, array [0], array [1], and array [2] exist. When extracting from the end, if the initial value of int i is the number of arrays, it will be extracted from array [3] and an error will occur. By incrementing -1, the initial values array [2] to [0] are output in descending order with i--.

Recommended Posts

Extract elements of array / extract in reverse order-java
12 of Array
Compare the elements of an array (Java)
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
[Swift] How to get the number of elements in an array (super basic)
Summary of how to select elements in Selenium
Extract elements in order from a class type ArrayList
[Ruby] The role of subscripts in learning elements in arrays
Examine the elements in the array using the [Ruby] includes? Method
I got stuck in a clone of a two-dimensional array
Summarize the additional elements of the Optional class in Java 9
Output in multiples of 3
Multidimensional array in Swift
[Ruby] Searching for elements in an array using binary search
How to add elements without specifying the length of the array
Generate Stream from an array of primitive types in Java