How to Find Out What Type Each Object Is in a Arraylist<Object>

How do I find out what type each object is in a ArrayListObject?

In C#:
Fixed with recommendation from Mike

ArrayList list = ...;
// List<object> list = ...;
foreach (object o in list) {
if (o is int) {
HandleInt((int)o);
}
else if (o is string) {
HandleString((string)o);
}
...
}

In Java:

ArrayList<Object> list = ...;
for (Object o : list) {
if (o instanceof Integer)) {
handleInt((Integer o).intValue());
}
else if (o instanceof String)) {
handleString((String)o);
}
...
}

Check which type of object List? contains

Generics only provide compile-time checks. At runtime, they are completely gone. This is known as type erasure. So at runtime, your code looks like this:

    List rhsList1 = new ArrayList();
rhsList1.add(segmentDetailInfo);

List rhsList2 = new ArrayList();
rhsList2.add(segReqInfoBean);

doProspecListCompareCheck(rhsList1);
doProspecListCompareCheck(rhsList2);

}
private static void doProspecListCompareCheck(Object rhsList) {

if (rhsList instanceof List) //wrong Check
//DoTHIS
else if(rhsList instanceof List) //wrong Check
//Do THIS

}

Distinguishing two generic objects by their generic parameter is simply not something you can do in Java.

java: how to check the type of an ArrayList as a whole

There is no such thing as 'the type' of an ArrayList.

The class ArrayList stores a list of Object references. It doesn't know and doesn't care if they are all of some type.

The Java generic system adds compile-time checking to help you keep track of types. So, if you declare ArrayList<Person>, you will get compile errors if you write code that could insert a non-Person into your list.

At runtime, the only way to tell what is in an ArrayList to iterate over all the contained items and check them with instanceof.

How to check the type of object in ArrayList

you can use the normal GetType() and typeof()

if( obj.GetType() == typeof(int) )
{
// int
}

How to use the ArrayList.contain() to check the object in java

Yes. You need to define your NimPlayer class like this (if you want to compare two NimPlayer objects only by their userName)

package com.example.schooltimetable;

import java.util.Objects;

public class NimPlayer {
private String userName;
private String familyName;
private String givenName;
private int gamesWon;
private int gamesPlayed;

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getFamilyName() {
return familyName;
}

public void setFamilyName(String familyName) {
this.familyName = familyName;
}

public String getGivenName() {
return givenName;
}

public void setGivenName(String givenName) {
this.givenName = givenName;
}

public int getGamesWon() {
return gamesWon;
}

public void setGamesWon(int gamesWon) {
this.gamesWon = gamesWon;
}

public int getGamesPlayed() {
return gamesPlayed;
}

public void setGamesPlayed(int gamesPlayed) {
this.gamesPlayed = gamesPlayed;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
NimPlayer nimPlayer = (NimPlayer) o;
return Objects.equals(userName, nimPlayer.userName);
}

@Override
public int hashCode() {
return Objects.hash(userName);
}
}

Now to check if the arraylist contains :

public void showCertainPlayerInformation(ArrayList<NimPlayer> playerList, String inputName)
{
NimPlayer n = new NimPlayer();
n.setUserName(inputName);
if(playerList.contains(n)){
....
return;
}
System.out.println("The player does not exist");
}

How to retrieve a value in ArrayListObject where it contains two different object type arraylist..?

You can do this things:

ArrayList<MyClass1> obj1 = new ArrayList<MyClass1>();
ArrayList<MyClass2> obj2 = new ArrayList<MyClass2>();

ArrayList<Object> obj3 = new ArrayList<Object>();

obj3.add(obj1);
obj3.add(obj2);

for (int = 0 ; i < obj3.size() ; i++) {
Object obj = obj3.get(i);
if (obj instanceof MyClass1) {
MyClass1 cls1 = (MyClass1)obj;
cls1.get()..//You getters and setters
}
if (obj instanceof MyClass2) {
MyClass2 cls2 = (MyClass2)obj;
cls2.get()..//You getters and setters
}
}

Or you can do something smarter and create an interface or Abstract Class/Base Class

public Interface Getable {
void getAttrbiute();
}

And than just implement in MyClass1, MyClass2.
In this case you can run over the list without the casting an the instaceof check

ArrayList<MyClass1> obj1 = new ArrayList<MyClass1>();
ArrayList<MyClass2> obj2 = new ArrayList<MyClass2>();

ArrayList<Getable> obj3 = new ArrayList<Getable>();

obj3.add(obj1);
obj3.add(obj2);

for (int = 0 ; i < obj3.size() ; i++) {
Getable objGetable = obj3.get(i);
objGetable.getAttrbiute();
}

Hope that helps..

Get specific objects from ArrayList when objects were added anonymously?

Given the use of List, there's no way to "lookup" a value without iterating through it...

For example...

Cave cave = new Cave();

// Loop adds several Parties to the cave's party list
cave.parties.add(new Party("FirstParty")); // all anonymously added
cave.parties.add(new Party("SecondParty"));
cave.parties.add(new Party("ThirdParty"));

for (Party p : cave.parties) {
if (p.name.equals("SecondParty") {
p.index = ...;
break;
}
}

Now, this will take time. If the element you are looking for is at the end of the list, you will have to iterate to the end of the list before you find a match.

It might be better to use a Map of some kind...

So, if we update Cave to look like...

class Cave {
Map<String, Party> parties = new HashMap<String, Party>(25);
}

We could do something like...

Cave cave = new Cave();

// Loop adds several Parties to the cave's party list
cave.parties.put("FirstParty", new Party("FirstParty")); // all anonymously added
cave.parties.put("SecondParty", new Party("SecondParty"));
cave.parties.put("ThirdParty", new Party("ThirdParty"));

if (cave.parties.containsKey("SecondParty")) {
cave.parties.get("SecondParty").index = ...
}

Instead...

Ultimately, this will all depend on what it is you want to achieve...

How to search for an Object in ArrayList?

created the list and the search item:

List<String> list = new ArrayList<String>();
list.add("book");
list.add("pencil");
list.add("note");

String itemToBeSearched = "book"; // taken as example

if(check(list,itemToBeSearched)){
System.out.println("ITEM FOUND");
}
else
{
System.out.println("NOT FOUND");
}

then the item check function is

public static boolean check(List<String> list, String itemToBeSearched){
boolean isItemFound =false;
for(String singleItem: list){
if(singleItem.equalsIgnoreCase(itemToBeSearched)){
isItemFound = true;
return isItemFound;
}
}
return isItemFound;
}

and it's working for me, please try this and let us know :)



Related Topics



Leave a reply



Submit