0

So, I had a quiz yesterday and I was given an Object type array with both int and char type elements(as below). I had to create a custom Linked List class and create a LinkedList using only the characters from the array. The problem which a faced was I could not differentiate between char and int as it was all 'Object' type. How can you convert an element from Object to an int or char? thanks a lot. the array I was given :

Objects[] array={1,32,c,f,16,-1,g,25,m,k};
Yaman Jain
  • 1,254
  • 11
  • 16
AMAKASH
  • 3
  • 2

1 Answers1

0

You can use :

myVar instanceOf Integer

to check if your variable is an Integer or a Character then you can cast your variable to the selected type

Integer myInt = (Integer) myVar;

More info here : What is the 'instanceof' operator used for in Java?

Lucas Duval
  • 188
  • 1
  • 7