简体   繁体   English

如何在主类中实现交换数组方法?

[英]How do I Implement swap array method in main class?

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[]);我在想这样的事情: theArray(0,9 arr[]);

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 ie they are one per class as compared to non- static members which are one per instance.请注意, static成员是类成员,即与每个实例一个的非static成员相比,它们每个类都有一个。 Therefore, static members should be accessed using the class name instead of using an instance eg Array.display as shown above.因此,应该使用类名而不是使用实例来访问static成员,例如Array.display如上所示。

Side note (because it won't affect the execution of your program): You should always follow Java naming conventions eg the class, array should be named as Array and the class, arrayTest should be named as ArrayTest .旁注(因为它不会影响您的程序的执行):您应该始终遵循Java 命名约定,例如类、 array应命名为Array和类、 arrayTest应命名为ArrayTest Also, try to avoid a name which has already been used in standard Java library eg you should choose a name other than Array .此外,尽量避免使用标准 Java 库中已经使用的名称,例如您应该选择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.如有任何疑问/问题,请随时发表评论。

无需方法交换只需执行以下操作:

a[i] = a[i]^a[j] ^(a[j]=a[i]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在数组中交换两个整数,而我的方法从main中获取两个整数和一个数组? - How do I swap two integers in an array, where my method takes in two integers and an array from main? 如何在主类中使用从方法到数组的字符? - How do I use characters from a method to an array in the main class? 如何编写通用方法来交换数组中项目的值? - How do I write a generic method to swap values of items in an array? 我如何将这些方法从 class 实现到主 class? - How do i implement these methods from a class into a main class? 从泛型类返回数组。如何在main方法中打印数组中的数据? - Returning an array from a generic class. How do I print the data from the array in main method? 如何在主循环中实现Graphics类? - How do I implement a Graphics class in my main loop? 如何在另一个类中调用一个类的main()方法? - How do I invoke a main() method of one class in another class? Java:如何在另一个扩展Thread的类中调用方法,并且该方法是我的主类中的Thread Array的一部分? - Java : How do I call a method in a different class that extends Thread and is a part of a Thread Array from my main class? 如何将main方法分成自己的类? - How do I seperate main method into its own class? 如何在主要方法Java中调用类? - How do i call a class in the main method java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM