Best strategies when calling a method that needs to change more than one variable

I am new to Java, I need to convert C / C ++ code to Java, and I run into obstacles. Due to the way variables are passed to methods, modifying them in a method is not simple, and I have no idea which approach is the most reasonable. Sorry for the pseudo-code examples, I hope they clearly explain what I'm talking about without going into unnecessary details.

I need something that would be equivalent to C

ModifyMyString(type1 &t1,type2 &t2);

(the return type does not matter, it may not be valid), since I need a function to change both t1 and t2.

I can easily change one of the variables, for example, t1, declaring in Java

type1 modifyMyString(type1 t1, type2 t2);    

and return value assignment

t1 = modifyMyString(t1,t2);

but this is only half the success, as the new value of t2 is lost.

I can declare a new class

class JustToPassTwoVariables {
    type1 t1;
    type2 t2;
    JustToPassTwoVariables(type1 tt1, type2 tt2) { t1 = tt1; t2 = tt2; }
}

-

JustToPassTwoVariables jtptv = modifyMyString(JustToPassTwoVariables(t1,t2));

, .

modifyMyString , modifyMyString, , JustToPassTwoVariables.

(, , , , ) Java?

+4
8

java ( ) , , .

, , , , , , .

+7

- . Java.

- , , . , , .

, .

, Java Inner Classes, .

+5

java. - values. .. , Result , , type1 type2. , , Result - type1 type2

:

Result result = modifyMyString(t1,t2);
result.getT1(); //gets t1 value
result.getT2(); // gets t2 value

, Java

+3

" /". , "", , // .. ", ".

,

List<String> l = new ArrayList<String>();
    l.add("Hello");
    l.add("world");
    ModifyMyString(l);
    // here also l = "hello" , "world" , "added"

    public void ModifyMyString(List l)
    {
     l.add("added"); // now l = "hello" , "world" , "added"
    }
+2

Java, , type1 type2 , . , . :

void myMethod(type1 arg0, type2 arg1) {
    arg0.setValue(newValue0);
    arg1.setValue(newValue1);
}

type1 / type2 (, String), - , .

class Type1Wrapper {
    private type1 type1;

    type1 getType1() {
        return type1;
    }

    void setType1(type1 newType1) {
        type1 = newType1;
    }
}

, ( void, )

+2

Java - OO, , , OO. , .

, t1 t2 / . , , , . , , , .

, , 1 2.

public class TypeContainer
{

  private String type1;
  private String type2;

  .. getters and setters 
}

, .

, JustToPassTwoVariables. OO. , , , OO .

...
public void modfiy(String val1, String val2)
{
  type1 = val1;
  type2 = val2;
}
...

, , , , setter. !

, - . , , , (, ).

C OO Java PITA. . , , , Java - . , .

+1

Java , in , C/++/#, " void , /", Custom "Reference" , .

public class CustomRef {
      public Object internal;

      public CustomRef(Object object) {
             this.internal=object;
      }


 }

,

 CustomRef ref1= new CustomRef(myParams1);
 CustomRef ref2= new CustomRef(myParams2);

 myFunction(ref1, ref2);

 myParams1 = ref1.internal;
 myParams2 = ref2.internal;

 void myFunction(CustomRef ref1, CustomRef  ref2) {

      Object param1 = ref1.internal 
      // a lot of code
      ref1.internal = param1;

 }

... , ArrayList, [] " ". : ; CustomRef ( ).

+1

StringBuffer. String String

-5
source

All Articles