0
public class array {
    public int[] Array;

    public array() {
        Array= new int[11];
    }

    public static void swap(int i, int j, int [] array) {
        int temp=array[i];
        array[i]=array[j];
        array[j]=temp;
    }
    ...
}

public class arrayTest {

   public static void main(String  [] args) {
        array theArray = new array();    
        theArray.display(); //display array
   }
   ...
}

I'm thinking something like this : theArray(0,9 arr[]);

ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
onlineuser
  • 13
  • 1
  • No. It's a `static` method. `array.swap(0,9,theArray.Array);` and your names are bad. – Elliott Frisch Mar 08 '20 at 21:01
  • Please do *NOT* use a "standard" name like "array" or "arraylist" for your own class, variable and method names! Please follow the standard conventions and capitalize the first letter of your class names (e.g. `class MyClass {...}`). Please use the "code" tag to format your code segments, instead of manually inserting back-ticks. – FoggyDay Mar 08 '20 at 21:02
  • Please consider using java's naming conventions. The code is conflicting where you have a class like `array` but an instance of that class as `Array`. – ChiefTwoPencils Mar 08 '20 at 21:02
  • Does this answer your question? [Effective swapping of elements of an array in Java](https://stackoverflow.com/questions/13766209/effective-swapping-of-elements-of-an-array-in-java) – Adithya Upadhya Mar 08 '20 at 21:04
  • how would I display after swapping? – onlineuser Mar 08 '20 at 21:06

2 Answers2

0

Given below is a sample implementation:

class Array {
    public static void swap(int i, int j, int[] array) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    /**
     * 
     * Displays elements of the array from the end to the beginning
     */
    public static void display(int[] array) {
        // Use `for (int i =0; i <array.length; i++)` for forward navigation
        for (int i = array.length - 1; i >= 0; i--) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }
}

public class ArrayTest {
    public static void main(String[] args) {
        int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        Array.display(arr);
        Array.swap(0, 9, arr);
        Array.display(arr);
    }
}

Output:

10 9 8 7 6 5 4 3 2 1 
1 9 8 7 6 5 4 3 2 10 

Notice that the static members are class members i.e. they are one per class as compared to non-static members which are one per instance. Therefore, static members should be accessed using the class name instead of using an instance e.g. Array.display as shown above.

Side note (because it won't affect the execution of your program): You should always follow Java naming conventions e.g. the class, array should be named as Array and the class, arrayTest should be named as ArrayTest. Also, try to avoid a name which has already been used in standard Java library e.g. you should choose a name other than Array.

[Update]

As promised, posting below a different implementation:

import java.util.Random;

class Array {
    int[] array;

    Array(int size) {
        array = new int[size];
        // Initialise the array with random elements from 0 to 100
        Random random = new Random();
        for (int i = 0; i < size; i++) {
            array[i] = random.nextInt(100);
        }
    }

    public void swap(int i, int j) {
        if (i < 0 || j > array.length - 1) {
            System.out.println("Indices should be in the range of 0 to " + (array.length - 1));
            return;
        }
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    /**
     * 
     * Displays elements of the array from the end to the beginning
     */
    public void display() {
        for (int i = array.length - 1; i >= 0; i--) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }
}

public class ArrayTest {
    public static void main(String[] args) {
        Array arry = new Array(10);
        arry.display();
        arry.swap(0, 9);
        arry.display();
        arry.swap(0, 11);
    }
}

Output from a sample run:

94 50 5 90 78 33 90 61 64 31 
31 50 5 90 78 33 90 61 64 94 
Indices should be in the range of 0 to 9

Note that these are just a couple of sample implementations. I hope, it will help how you write your own implementation as per your requirement. Feel free to comment in case of any doubt/issue.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • i have a display method that prints 1 to 10 but backward public void display(){ // int[] arr=new int[11]; for(int i=Array.length-1;i>=1; i--){ Array[i]=i; System.out.print ( Array[i] + "," ); } } – onlineuser Mar 08 '20 at 21:12
  • I've updated my answer to display the elements in the backwards direction. I hope, it meets your requirement now. – Arvind Kumar Avinash Mar 08 '20 at 21:20
  • Thank you! can u explain why in the main class you created the "int [ ] arr.." – onlineuser Mar 08 '20 at 21:22
  • This way, you do not have always the same array to be swapped and displayed. You can pass a different array to be swapped or displayed depending on your requirement. I will post a different implementation as well in a few minutes. – Arvind Kumar Avinash Mar 08 '20 at 21:27
0

no need for method to swap just do:

a[i] = a[i]^a[j] ^(a[j]=a[i]);
Dharman
  • 30,962
  • 25
  • 85
  • 135
digitebs
  • 834
  • 8
  • 10