Return of java method values



  • Is there a method that changes the location of the first and last element of the mass, it returns the meaning of the first element, can we get two values back? Or do you need to write two different methods?

    public static void main(String[] args) {
    
    int array1[] = new int[]{1,5,3,6,2,5,4};
    
    System.out.println(replacePlaces(array1));
    

    }

    public static int replacePlaces(int [] array){

    int firstElement = array[0];
    array[0] = array[array.length-1];
    array[array.length-1] = firstElement;
    
    
    return firstElement;
    

    }



  • I don't know why it's 2 but this is:

    public static void main(String[] args) {
    
    int array1[] = new int[]{1,5,3,6,2,5,4};
    
    MyData data = replacePlaces(array1);
    System.out.println(data.first + ", " + data.second);
    

    }

    public static MyData replacePlaces(int [] array){

    int firstElement = array[0];
    array[0] = array[array.length-1];
    array[array.length-1] = firstElement;
    
    
    return new MyData(firstElement, array[array.length-1]);
    

    }

    public class MyData{
    public int first;
    public int second;
    public MyData(int first, int second){
    this.first = first;
    this.second = second;
    }
    }


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2