如何使用不同的参数将 class 调用到另一个?
How do I call a class into another with different parameters?
我不确定如何调用我的代码以使其与我的随机发生器相匹配。我的随机发生器基本上在一个数组中生成一些随机整数并像这样打印它们。
编辑:如果我不清楚,我想将我的随机化器调用到插入排序的两种方法中 class!
我尝试使用从 geeks4geeks 获取的插入排序实现。它们都有参数 int[arr]。
这是我的代码
public class randomArr {
public void randomizer(){
Scanner sc = new Scanner(System.in);
Random r = new Random();
System.out.println("Please enter the amount of integers you want in your array: ");
int n = sc.nextInt();
// Create array of 1000 ints
int[] intArr = new int[n];
// Fill array with random ints
for ( int i = 0; i < intArr.length; i++) {
intArr[i] = r.nextInt(100);
System.out.print(intArr[i] + ", ");
}
}
}
这基本上是我想要发生的事情:
如您所见,我正在使用 "intArr" 调用排序和打印数组。我如何使用给定的方法调用我的方法?
我已经注释掉了我使用我的方法创建的对象,但如果我将其保留在其中,我将如何使用该对象调用 sort() 和 printarray()?
public class insertionSort {
public static void main(String[] args){
//randomArr arry = new randomArr(); \the object of my method
//arry.randomizer());
Scanner sc = new Scanner(System.in);
Random r = new Random();
System.out.println("Please enter the amount of integers you want in your array: ");
int n = sc.nextInt();
// Create array of 1000 ints
int[] intArr = new int[n];
// Fill array with random ints
for ( int i = 0; i < intArr.length; i++) {
intArr[i] = r.nextInt(100);
System.out.print(intArr[i] + ", ");
}
sort(intArr);
printArray(intArr);
}
public static void sort(int arr[]){
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
static void printArray(int arr[]){
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + ", ");
System.out.println();
}
}
好的,如果你想调用一个 class 的方法到另一个,你只需要通过在那个 class 的对象旁边添加方法名称来调用它,并在两者之间添加一个句点像这样 ("object.method()")。所以你会做的是:
import java.util.*;
class randomArr {
public int[] randomizer()
{
Scanner sc = new Scanner(System.in);
Random r = new Random();
System.out.println("Please enter the amount of integers you want in your array: ");
int n = sc.nextInt();
// Create array of 1000 ints
int[] intArr = new int[n];
// Fill array with random ints
for ( int i = 0; i < intArr.length; i++) {
intArr[i] = r.nextInt(100);
System.out.print(intArr[i] + ", ");
}
return intArr;
}
}
class Stack
{
public static void main(String[] args)
{
randomArr rArr = new randomArr();
int[] intArr = rArr.randomizer();
}
}
通过这种方式,您可以在另一个 class 中创建该数组并在新的 class 中使用它。
如果你想调用 insertionSort 的方法 class 你可以在没有对象的情况下调用它们,因为它们是静态的。您不能使用另一个 class 的对象来调用不属于该对象的 class 的方法。
您可以在 randomArr class 中使用 insertionSort 的方法,方法是在 randomArr class 中创建它的对象,反之亦然。但是一个对象只能访问它自己的方法 class.
你想在另一个 class 中调用方法,你可以创建那个 class 的对象,然后使用对象引用你可以调用那个 class 中可用的方法。
insertionSort object = new insertionSort();
object.sort(intArr);
object.printArray(intArr);
由于您定义的 sort 和 printArry 是静态方法,您可以直接在 main 方法中调用它们。
我不确定如何调用我的代码以使其与我的随机发生器相匹配。我的随机发生器基本上在一个数组中生成一些随机整数并像这样打印它们。
编辑:如果我不清楚,我想将我的随机化器调用到插入排序的两种方法中 class!
我尝试使用从 geeks4geeks 获取的插入排序实现。它们都有参数 int[arr]。
这是我的代码
public class randomArr {
public void randomizer(){
Scanner sc = new Scanner(System.in);
Random r = new Random();
System.out.println("Please enter the amount of integers you want in your array: ");
int n = sc.nextInt();
// Create array of 1000 ints
int[] intArr = new int[n];
// Fill array with random ints
for ( int i = 0; i < intArr.length; i++) {
intArr[i] = r.nextInt(100);
System.out.print(intArr[i] + ", ");
}
}
}
这基本上是我想要发生的事情:
如您所见,我正在使用 "intArr" 调用排序和打印数组。我如何使用给定的方法调用我的方法?
我已经注释掉了我使用我的方法创建的对象,但如果我将其保留在其中,我将如何使用该对象调用 sort() 和 printarray()?
public class insertionSort {
public static void main(String[] args){
//randomArr arry = new randomArr(); \the object of my method
//arry.randomizer());
Scanner sc = new Scanner(System.in);
Random r = new Random();
System.out.println("Please enter the amount of integers you want in your array: ");
int n = sc.nextInt();
// Create array of 1000 ints
int[] intArr = new int[n];
// Fill array with random ints
for ( int i = 0; i < intArr.length; i++) {
intArr[i] = r.nextInt(100);
System.out.print(intArr[i] + ", ");
}
sort(intArr);
printArray(intArr);
}
public static void sort(int arr[]){
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
static void printArray(int arr[]){
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + ", ");
System.out.println();
}
}
好的,如果你想调用一个 class 的方法到另一个,你只需要通过在那个 class 的对象旁边添加方法名称来调用它,并在两者之间添加一个句点像这样 ("object.method()")。所以你会做的是:
import java.util.*;
class randomArr {
public int[] randomizer()
{
Scanner sc = new Scanner(System.in);
Random r = new Random();
System.out.println("Please enter the amount of integers you want in your array: ");
int n = sc.nextInt();
// Create array of 1000 ints
int[] intArr = new int[n];
// Fill array with random ints
for ( int i = 0; i < intArr.length; i++) {
intArr[i] = r.nextInt(100);
System.out.print(intArr[i] + ", ");
}
return intArr;
}
}
class Stack
{
public static void main(String[] args)
{
randomArr rArr = new randomArr();
int[] intArr = rArr.randomizer();
}
}
通过这种方式,您可以在另一个 class 中创建该数组并在新的 class 中使用它。
如果你想调用 insertionSort 的方法 class 你可以在没有对象的情况下调用它们,因为它们是静态的。您不能使用另一个 class 的对象来调用不属于该对象的 class 的方法。
您可以在 randomArr class 中使用 insertionSort 的方法,方法是在 randomArr class 中创建它的对象,反之亦然。但是一个对象只能访问它自己的方法 class.
你想在另一个 class 中调用方法,你可以创建那个 class 的对象,然后使用对象引用你可以调用那个 class 中可用的方法。
insertionSort object = new insertionSort();
object.sort(intArr);
object.printArray(intArr);
由于您定义的 sort 和 printArry 是静态方法,您可以直接在 main 方法中调用它们。