I am newbie in java. I need to pass primitive type value as a reference in function call.I dont want to return i value from function because it already returns some object.
for (int i = 0; i < objects.size();) {
// here it should fetch that object based on i manipulated in function
Object obj = objects.get(i)
Object node = someFunction(session,obj,i);
// push node in nodes array
}
public Object someFunction(Session session,Object obj,int i){
//manipulate i value based on condition
if(true){
i = i + 1;
}else{
i = i + 2;
}
}
How i can achieve this as JAVA use pass by value in function call?
Thanks
In java primitive types always pass by value. To pass by reference you should define a class and put your primitive type in it. If you pass Integer class it doesn't work because this class is immutable and value doesn't change.
You can use a singular array of type int[]
for a quick solution and increase its internal value. That doesn't change the array itself, only its content.
Are you aware of java streams? With streams you can do something like:
List<Object> result = objects.stream()
.filter(object -> {/*add condition here*/})
.map(object->{/*do something with object that match condition above*/})
.collect(Collectors.toList());
You can use this mechanism to collect and process objects based on certain conditions.
If that doesn't help, maybe use an iterator?
Iterator<Object> it = objects.iterator();
while(it.hasNext()){
Object node = someFunction(session,it);
}
public Object someFunction(Session session,Iterator i){
//manipulate i value based on condition
if(true){
i.next();
}else{
i.next();
i.next();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.