Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA传递原语类型作为函数调用的引用_Java_Pass By Reference_Primitive Types - Fatal编程技术网

JAVA传递原语类型作为函数调用的引用

JAVA传递原语类型作为函数调用的引用,java,pass-by-reference,primitive-types,Java,Pass By Reference,Primitive Types,我是java的新手。我需要在函数调用中将基元类型值作为引用传递。我不想从函数返回I值,因为它已经返回了一些对象 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); //

我是java的新手。我需要在函数调用中将基元类型值作为引用传递。我不想从函数返回I值,因为它已经返回了一些对象

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;
   }
}
for(int i=0;i
当JAVA在函数调用中使用传递值时,如何实现这一点


谢谢

在java中,基本类型总是按值传递。要通过引用传递,应该定义一个类并将基本类型放入其中。如果传递Integer类,它将不起作用,因为该类是不可变的,并且值不会更改。

可以使用
int[]
类型的单数数组快速求解并增加其内部值。这不会改变数组本身,只会改变其内容。

您知道java流吗?使用streams,您可以执行以下操作:

List<Object> result = objects.stream()
     .filter(object -> {/*add condition here*/})
     .map(object->{/*do something with object that match condition above*/})
     .collect(Collectors.toList()); 
List result=objects.stream()
.filter(对象->{/*在此处添加条件*/})
.map(对象->{/*对匹配上述条件的对象执行操作*/})
.collect(Collectors.toList());
您可以使用此机制根据特定条件收集和处理对象

如果这没有帮助,也许可以使用迭代器

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();
   }
}
Iterator it=objects.Iterator();
while(it.hasNext()){
对象节点=someFunction(会话,it);
}
公共对象函数(会话,迭代器i){
//基于条件操纵i值
如果(真){
i、 next();
}否则{
i、 next();
i、 next();
}
}

我不知道你想做什么。无论哪种方式,对于每个原语,都有一个包装器类。对于int,它是整数。你查过了吗?将原语作为引用传递,这对你有意义吗?@Eugene你能添加一些例子吗?你要找的单词是instance variable然后返回(并赋值)新值,或者使用一个实例变量而不是局部变量。它总是按值传递,对于对象,它只是引用的值