我有三個java文件,我正在使用我似乎無法讓我的數組打印。我嘗試了各種各樣的東西,看起來好像我在圈子裏。我對如何處理來自不同類的數組感到困惑。預先感謝您的任何幫助。傳遞類之間的數組...卡住
這裏是主:
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
TestInteger TI = new TestInteger();
TestDouble TD = new TestDouble();
Distance TDist = new Distance();
mainMenu();
}
public static void mainMenu() {
int option;
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("Please choose what type of numbers will be stored in the Array");
System.out.println();
System.out.println("******* MAIN MENU ********");
System.out.println("** 1. Integers **");
System.out.println("** 2. Double **");
System.out.println("** 3. Distance **");
System.out.println("** 4. Exit **");
System.out.println("**************************");
try{
option = sc.nextInt();
switch (option) {
case 1: {
TestInteger.arrayMenu();
}
case 2: {
TestDouble.arrayMenu();
}
case 3: {
Distance.arrayMenu();
}
case 4: {
System.out.println("Exiting Program");
System.exit(0);
}
default: {
System.out.println();
System.out.println("Invalid option. Please select option" + " 1 - 4");
System.out.println();
mainMenu();
}
}
}catch(InputMismatchException e){
System.out.println("Enter only 1-4");
sc.nextLine();
}
}
}
}
的SortedArray類應該持有對象的數組排序的所有方法與SortedArray使用。
import java.util.Arrays;
public class SortedArray implements Comparable {
//private int[] sa;
//public SortedArray(int i, int j) {
// TODO Auto-generated constructor stub
//}
public void constructor() {
final int initialSize = 5;
int incrementAmount = 3;
int top = -1;
Comparable [] sa = new Comparable[initialSize];
//for(int i=0; i<5;++i){
// SortedArray sa = new SortedArray();
//}
top = -1; //shows the last item of the array
}
public int appropriatePosition(){
int ap = 0;
return ap;
}
public int smallest(){
int smallest = 0;
System.out.println("The smallest is ");
return smallest;
}
public int largest(){
int largest = 0;
System.out.println("The largest is ");
return largest;
}
public void insert(int i){
int pos = 0;
pos++;
}
public void find(){
}
public void delete(){
}
public Comparable[] print(){
Comparable[] sa = {2,3,4,5};
System.out.println(sa);
return sa;
}
public void clear(){
System.out.println("Now Clearing the Array....................................");
// Arrays.fill(sa, null);
}
public boolean full(){
boolean full = false;
return full;
}
public boolean empty(){
boolean empty = false;
return empty;
}
@Override
public int compareTo(Object arg0) {
// TODO Auto-generated method stub
return 0;
}
TestInteger類僅用於將整數插入到數組中,然後以不同方式操作數組。
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;
public class TestInteger implements Comparable{
/**
* This is the User Interface for manipulating the sorted array
*/
static SortedArray sa = new SortedArray (5,3);
public static void arrayMenu() {
int option;
while(true){
System.out.println();
System.out.println("**** Integer Array Menu ****");
System.out.println("****************************");
System.out.println("** 1. Insert **");
System.out.println("** 2. Delete **");
System.out.println("** 3. Clear **");
System.out.println("** 4. Smallest **");
System.out.println("** 5. Largest **");
System.out.println("** 6. Return to Main Menu **");
System.out.println("****************************");
Scanner sc = new Scanner(System.in);
try{
option = sc.nextInt();
switch (option){
case 1:{
try{
System.out.println("Type an integer to insert: ");
int x = sc.nextInt();
int index = 0;
sa.insert(x);
sa.print();
}catch(InputMismatchException e){
System.out.println("Enter only integers");
sc.nextLine();
}
arrayMenu();
}
case 2:{
try{
System.out.println("Type the index of the item you wish to delete:");
int d = sc.nextInt();
}catch(InputMismatchException e){
System.out.println("Enter only integers");
sc.nextLine();
}
arrayMenu();
}
case 3:{
System.out.println("Before clearing");
sa.print();
sa.clear();
System.out.println("After clearing");
sa.print();
arrayMenu();
}
case 4:{
sa.smallest();
arrayMenu();
}
case 5:{
sa.largest();
arrayMenu();
}
case 6:{
Main.mainMenu();
}
default: {
System.out.println();
System.out.println("Invalid option. Please select option 1 - 6");
System.out.println();
arrayMenu();
}
}
}catch(InputMismatchException e){
System.out.println("Enter only 1-6");
sc.nextLine();
}
}
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
return 0;
}
}
確實SortedArray sa = new SortedArray();實際上做什麼?我相當肯定構造函數不能被命名爲「構造函數」... –
我評論說,因爲我得到奇怪的錯誤。好吧,我會改變構造函數的方法名稱。 – Neophyte