简体   繁体   English

不能使用类型转换将通用类“新建对象”

[英]Can't “new object” for generic class with type casting

This code is for learning generic class and stack operation. 该代码用于学习通用类和堆栈操作。 but it has some error about declare variable type 但是关于声明变量类型有一些错误

package lab11_2_590510535;
import java.util.Scanner;

class Queue <TYPE>{
    private int count;
    private int front;
    private int rear;
    private int n;
    private Object [] item;
    private TYPE queueFront;
    static void pl(Object a){System.out.println(a);}
    static void p(Object a){System.out.print(a);}

Queue(int x){
    n = x;
    item = new Object[n];
    front = 0;
    rear = -1;
    count = 0;
}

public boolean isEmpty(){
    for(int i = 0 ; i<item.length ; i++){
        if(item[i] != null){return false;}
    }
    return true;
}

public boolean isFull(){
    if(item[item.length] != null){return true;}
    else{return false;}
}

public void enqueue(TYPE v){
    if(!isFull()){
        if(rear < n-1){
            rear++;
            item[rear] = v;
            count++;
        }
    }else{pl("Queue is Full.");}
}
public TYPE dequeue(){
    if(!isEmpty()){
        queueFront = item[front];
        front++;
        count++;
    }else{p("Queue is empty.");}
    return queueFront;
}
public void show(){
 for(int i = 0 ; i <item.length ; i++){
     if(item[i] !=  null){p(item[i] + ", ");}
 }
}


public class Lab11_2_590510535 {
    static void pl(Object a){System.out.println(a);}
    static void p(Object a){System.out.print(a);}
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    Scanner keyboard = new Scanner(System.in);
    char choice;
    int N;
    p("Enter N: ");
    N = keyboard.nextInt();
    p("Choice input type; int(1) , char(2): ");
    choice = keyboard.next().charAt(0);
    if(choice == '1'){Queue <Integer> q = new Queue(N);}
    else if(choice == '2'){Queue <Character> q = new Queue(N);}
    do{
        pl("1) enqueue"); pl("2) dequeue"); pl("3) show"); pl("4) exit");
        p("Enter function number : ");
        choice = keyboard.next().charAt(0);
        if(choice == '1'){
            p("Enter data: ");
            Object s = keyboard.next();
            q.enqueue(s);
        }
        else if(choice == '2'){Object s =  q.dequeue();
            pl(s);
        }
        else if(choice == '3'){q.show();}
    }while(choice != '4');

    }
}

1.After user input choice and I create generic object with type casting in do...while loop it can't find "q". 1.选择用户输入后,我在do ... while循环中使用类型转换创建泛型对象,找不到“ q”。

2.In public TYPE dequeue() method line "queueFront = item[front];" 2.在公共TYPE dequeue()方法行中,“ queueFront = item [front];” Object can't be convert to TYPE ,how can I fix it. 无法将对象转换为TYPE,我该如何解决。

Try to change private Object [] item to private TYPE [] item . 尝试将private Object [] item更改为private TYPE [] item Also you will need to know how to create a generic array for your example: How to create a generic array? 另外,您将需要知道如何为示例创建通用数组如何创建通用数组? How to create a generic array in Java? 如何在Java中创建通用数组?

You will need to change item = new Object[n]; 您将需要更改item = new Object[n]; to item = (TYPE[]) new Object[n]; item = (TYPE[]) new Object[n];

However you should avoid creating generic types and instead you should inject them or use a factory to create them. 但是,应避免创建泛型类型,而应注入它们或使用工厂来创建它们。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM