Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

266
Visualizações
How to function a value in a Generic function in java?

Getting error: incompatible types: int cannot be converted to T. I want to build a queue using linked list that can store items of different data types. Please suggest ways on how can i pass values belonging to different data types into the generic function add().

public void main(String args[])
{
    MyQueue<T> q=new MyQueue<T>();
    q.add(10);
    q.add("Hello");
}

public void add(T item)
{
    QueueNode<T> t=new QueueNode<T>(item);
    if(last!=null)
    {
        last.next=t;
    }
    last=t;
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

T is a placeholder for a Type, but you can't declare T like this since T must be a known type. You want something like this. Here T is a known type which is passed to QueueNode and MyQueue.

package com.company;

import java.util.ArrayList;
import java.util.List;

class QueueNode<T> {
    private T nodeVal;

    T getNodeVal() {
        return nodeVal;
    }

    void setNodeVal(T nodeVal) {
        this.nodeVal = nodeVal;
    }

    QueueNode(T nodeVal) {
        this.nodeVal = nodeVal;
    }
}

class MyQueue<T> {
    private List<QueueNode<T>> actualQueue = new ArrayList<QueueNode<T>>();

    public List<QueueNode<T>> getActualQueue() {
        return actualQueue;
    }

    public void enqueue(T t) {
        actualQueue.add(new QueueNode<>(t));
    }

    public QueueNode<T> dequeue() {
        return actualQueue.remove(0);
    }
}

class Main {

    public static void main(String[] args) {

        MyQueue<Integer> integerQueue = new MyQueue<Integer>();
        integerQueue.enqueue(1);
        integerQueue.enqueue(2);
        integerQueue.enqueue(3);
        integerQueue.getActualQueue().forEach(e -> System.out.print(e.getNodeVal() + " ")); //prints 1 2 3
        System.out.println(); 
        integerQueue.dequeue();
        integerQueue.getActualQueue().forEach(e -> System.out.print(e.getNodeVal() + " ")); //prints 2 3
        System.out.println(); 
        integerQueue.dequeue();
        integerQueue.getActualQueue().forEach(e -> System.out.print(e.getNodeVal() + " ")); //prints 3
        System.out.println(); 
    }
}


over 4 years ago · Santiago Trujillo Relatório

0

You should change de T for Object. This way you can place whatever data type you want and then you can use a foreach, for example:

public static void main(String args[])
{
    Queue<Object> queues=new LinkedList<>();
    queues.add(10);
    queues.add("Hello");

    for(Object queue:queues){
        System.out.println(queue);
    }
}

The Generic class gives solution more bigger.

Also consider that int is a primitive data type and it's not a class. In this case Integer is the class that uses int.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda