+ 1

Increment pointer

What is the right syntax in java to increament an array reference like in C? (edited): what I mean by increament is for example when you have an array of n element and arr[0]=f arr[1]=l , arr[2]=k... When we increament by one it bacome arr[0]=l, arr[1]=k... and so on I want to know if there is a syntax to do this or i should create a method by my own? https://code.sololearn.com/cvgGZ1boeRmz/?ref=app

7th Nov 2021, 12:11 PM
Hadjer
Hadjer - avatar
4 Answers
+ 6
As far as I know Java doesn’t have pointers like some other languages do e.g., C. Thus, you won’t have direct access to memory or perform any pointer arithmetic operations. This is one of the reasons Java is managed and considered safer yet slow language.
7th Nov 2021, 12:25 PM
Flash
+ 5
import java.util.Arrays; public class Program { public static void main(String[] args) { int[] arr = {0, 1,2 ,3,4,5}; System.out.println (Arrays.toString(arr)); for (int i = 0; i < arr.length; i++) arr[i]++; System.out.println(Arrays.toString(arr)); } } // Keep learning & happy coding :D https://code.sololearn.com/cecN9JbBw9Nb
7th Nov 2021, 1:00 PM
SoloProg
SoloProg - avatar
+ 4
for (int i = 0; i < arr.length; i++) { arr[i]++; }
7th Nov 2021, 12:24 PM
JaScript
JaScript - avatar
+ 2
You can use Arrays.copyOfRange() method to shift the array indices, by creating a new array. The new array can also be the same size as the original, in this case, at least for int[] the remaining elements at the end are filled with 0. https://code.sololearn.com/c8bsVLDYVZLv/?ref=app https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Arrays.html#copyOfRange(int%5B%5D,int,int)
8th Nov 2021, 6:03 AM
Tibor Santa
Tibor Santa - avatar