[英]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以外的名称。
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.