Java将对象传递给方法
我有一个程序,允许用户在二叉搜索树、splay树和红黑树之间进行选择。我为二叉搜索树编写了这个类,现在我正在处理splay树,但是我意识到我与用户交互的方法只适用于二叉搜索树。我对它进行了设置,这样它将创建用户选择的任意树的实例,但在我的代码中,我只使用了如果用户选择了二叉搜索树将创建的变量。我的问题是,如何才能使它只创建用户选择的树的一个实例,以及如何仅使用一个变量,以便在插入项目或处理树时,不必为不同的树添加更多的条件语句 这就是我现在拥有的Java将对象传递给方法,java,binary-search-tree,splay-tree,Java,Binary Search Tree,Splay Tree,我有一个程序,允许用户在二叉搜索树、splay树和红黑树之间进行选择。我为二叉搜索树编写了这个类,现在我正在处理splay树,但是我意识到我与用户交互的方法只适用于二叉搜索树。我对它进行了设置,这样它将创建用户选择的任意树的实例,但在我的代码中,我只使用了如果用户选择了二叉搜索树将创建的变量。我的问题是,如何才能使它只创建用户选择的树的一个实例,以及如何仅使用一个变量,以便在插入项目或处理树时,不必为不同的树添加更多的条件语句 这就是我现在拥有的 import java.util.Scanner
import java.util.Scanner;
import java.lang.Math.*;
public class Driver1
{
public static void main(String[] args)
{
//local variables
String treeChoice = null;
String choice = null;
String choice2 = null;
String command = null;
int insertAmount = -1;
String pattern;
int height = -1;
int i = -1;
//BST<Integer> myTree = null;
//ST<Integer> mySTTree = null;
int num = 0;
//Scanners to take user input
Scanner input = new Scanner(System.in);
Scanner inputt = new Scanner(System.in);
System.out.println("Which tree would you like to test (BST, ST, RBT)? ");
treeChoice = input.nextLine();
//Based on user input either a BST, Splay Tree, or RBT will be initialized.
if("BST".equalsIgnoreCase(treeChoice))
{
BST<Integer> myTree = new BST<Integer>();
}
else if("ST".equalsIgnoreCase(treeChoice))
{
//System.out.println("Splay Tree not ready yet");
ST<Integer> mySTTree = new ST<Integer>();
}
else if("RBT".equalsIgnoreCase(treeChoice))
{
System.out.println("RBT not ready yet");
//RBT<Integer> myTree = new RBT<Integer>();
}
else
{
System.out.println("Invalid Entry");
}
//Ask user how many items to input
System.out.println("How many items would you like to insert? ");
insertAmount = input.nextInt();
//ask user if the items will be random or sorted
System.out.println("Pattern (random or sorted): ");
choice2 = inputt.nextLine();
//If random, create random numbers
if("random".equalsIgnoreCase(choice2))
{
for(i = 1; i <= insertAmount; i++)
{
myTree.insert((int)(Math.random()*1000000)+i);
}
}
//else fill the tree with numbers in order from 1 to the user limit
else if("sorted".equalsIgnoreCase(choice2))
{
for(i = 1; i <= insertAmount; i++)
{
myTree.insert(i);
}
}
//Keep asking users input on what to do to the tree until user says quit
while(command != "quit")
{
System.out.println(
"Next command (insert X, delete X, find X, height, quit)?");
command = inputt.nextLine();
if (command.startsWith("insert"))
{
num = Integer.parseInt(command.replaceAll("\\D", ""));
boolean result = myTree.insert(num);
if(result == false)
{
System.out.println("Item already present.");
}
}
else if(command.startsWith("delete"))
{
num = Integer.parseInt(command.replaceAll("\\D", ""));
boolean result = myTree.delete(num);
}
else if(command.startsWith("find"))
{
num = Integer.parseInt(command.replaceAll("\\D", ""));
boolean result = myTree.find(num);
if(result == true)
{
System.out.println("Item present.");
}
else
{
System.out.println("Item not present.");
}
}
else if(command.startsWith("height"))
{
System.out.println("Current height of tree " + myTree.height());
}
else if(command.startsWith("quit"))
{
break;
}
System.out.println();
}
}//Close main method
import java.util.Scanner;
导入java.lang.Math.*;
公共类驱动程序1
{
公共静态void main(字符串[]args)
{
//局部变量
字符串treeChoice=null;
字符串选择=null;
字符串choice2=null;
字符串命令=null;
int insertAmount=-1;
字符串模式;
整数高度=-1;
int i=-1;
//BST myTree=null;
//ST mySTTree=null;
int num=0;
//扫描仪以获取用户输入
扫描仪输入=新扫描仪(System.in);
扫描仪输入=新扫描仪(系统输入);
println(“您希望测试哪个树(BST、ST、RBT)?”;
treeChoice=input.nextLine();
//根据用户输入,将初始化BST、Splay树或RBT。
如果(“BST”。等信号情况(treeChoice))
{
BST myTree=new BST();
}
否则,如果(“ST”等信号情况(treeChoice))
{
//System.out.println(“Splay Tree尚未就绪”);
ST mySTTree=new ST();
}
else if(“RBT”。等效信号情况(treeChoice))
{
System.out.println(“RBT尚未准备就绪”);
//RBT myTree=新RBT();
}
其他的
{
System.out.println(“无效条目”);
}
//询问用户要输入多少项
System.out.println(“您希望插入多少项?”);
insertAmount=input.nextInt();
//询问用户项目是随机的还是排序的
System.out.println(“模式(随机或排序):”;
choice2=input.nextLine();
//如果是随机的,则创建随机数
如果(“随机”。相等信号情况(选择2))
{
对于(i=1;i您的树应该扩展一个公共基类,或者更好,实现一个公共接口,比如Tree
,该接口指定在所有树上使用的方法(find
,insert
,delete
)。那么您应该只有一个变量Tree myTree
,您可以将用户选择的类型的实际实例分配给该变量
但是,你确定上面的代码有效吗?如果你这样做
if("BST".equalsIgnoreCase(treeChoice))
{
BST<Integer> myTree = new BST<Integer>();
}
我非常建议您为类提供真实的名称,以明确它们所代表的内容,而不仅仅是两个或三个字母的组合。请注意,如果您没有在最后一个else
分支中抛出异常,编译器稍后会抱怨“变量myTree可能尚未初始化”
或者,您可以将树创建if else语句之后的所有代码放入一个方法中,比如说void testTree(tree myTree)
,并在评估用户输入的地方直接调用此方法,例如if(“BST”.equalsIgnoreCase(treeChoice))testTree(new BinarySearchTree())您的树应该扩展一个公共基类,或者更好,实现一个公共接口,比如说树,它指定了所有树上要使用的方法(查找,插入,删除)。那么您应该只有一个变量Tree myTree
,您可以将用户选择的类型的实际实例分配给该变量
但是,你确定上面的代码有效吗?如果你这样做
if("BST".equalsIgnoreCase(treeChoice))
{
BST<Integer> myTree = new BST<Integer>();
}
我非常建议您为类提供真实的名称,以明确它们所代表的内容,而不仅仅是两个或三个字母的组合。请注意,如果您没有在最后一个else
分支中抛出异常,编译器稍后会抱怨“变量myTree可能尚未初始化”
或者,您可以将树创建if else语句之后的所有代码放入一个方法中,比如说void testTree(tree myTree)
,并在评估用户输入的地方直接调用此方法,例如if(“BST”.equalsIgnoreCase(treeChoice))testTree(new BinarySearchTree());
,但在某些情况下,您仍然希望将其分配给变量。在行树中,树将是什么?@user214577定义树类共有的方法的接口(如第一句所述)。请参阅,例如,。在行树中,树是什么?@user214577定义树类共有的方法的接口(如第一句所述)。请参阅,例如。