Get Specific Arraylist Item

Get specific ArrayList item

As many have already told you:

mainList.get(3);

Be sure to check the ArrayList Javadoc.

Also, be careful with the arrays indices: in Java, the first element is at index 0. So if you are trying to get the third element, your solution would be mainList.get(2);

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...

Arraylist - how to get a specific element/name?

As others have said this isn't all that efficient and a HashMap will give you fast lookup. But if you must iterate over the list you would do it like this:

    String targetName = "Jane";
Customer result = null;
for (Customer c : list) {
if (targetName.equals(c.getName())) {
result = c;
break;
}
}

If you need to remove an item from a list while iterating over it you need to use an iterator.

    String targetName = "Jane";
List<Customer> list = new ArrayList<Customer>();
Iterator<Customer> iter = list.iterator();
while (iter.hasNext()) {
Customer c = iter.next();
if (targetName.equals(c.getName())) {
iter.remove();
break;
}
}

How can I get only specific values from an Arraylist

In Java you can create a sublist (javadoc) of a list; e.g.

List<Integer> list = ...
List<Integer> sublist = list.sublist(2, 6);

Notes:

  1. The the upper bound is exclusive, so that to get the list element containing 120 we must specify 6 as the upper bound, not 5.

  2. The resulting sublist is "backed" by the original list. Therefore:

    • there is no copying involved in creating the sublist, and
    • changes to the sublist will modify the corresponding positions in the original list.

How To Get A Specific list or item in an ArrayList

List of criminal names:

List<String> criminalNames = Arrays.asList("Joker","Morris");

Assuming that you have list of data:

List<Model> arrayList = new ArrayList<>();
arrayList.add(new Model("Joker", 29));
arrayList.add(new Model("Morris", 30));
arrayList.add(new Model("Joker", 30));
arrayList.add(new Model("Morris", 20));
arrayList.add(new Model("James", 18));
arrayList.add(new Model("Bond", 18));
arrayList.add(new Model("007", 19));

Get the crime list:

 List<Model> crimeList = new ArrayList<>();
for(Model model : arrayList){
if(criminalNames.contains(model.getName())){
crimeList.add(model);
}
}

Get the normal list:

 List<Model> normalList = new ArrayList<>();
for(Model model : arrayList){
if(!criminalNames.contains(model.getName())){
normalList.add(model);
}
}

Print results:

System.out.println("Crime list: " + crimeList);
System.out.println("Normal List: " + normalList);

A more elegant solution is to use Java stream API stream().filter:

List<Model> crimeList = arrayList.stream()
.filter(model -> criminalNames.contains(model.getName()))
.collect(Collectors.toList());

List<Model> normalList = arrayList.stream()
.filter(model -> !criminalNames.contains(model.getName()))
.collect(Collectors.toList());

get a specific object in arrayList

It seems like you are trying to find a specific school in the list of schools. If this is not what you are trying to do, please let me know.

Here's how I would do it:

public School findSchool(String schoolName)
{
// Goes through the List of schools.
for (School i : schools)
{
if (i.getName.equals(schoolname))
{
return i;
}
}
return null;
}

How to get a specific element from an arraylist that has string and int values from another class

Check if the the your list has any item present or no. If present, retrieve the first element and get the name for it to return otherwise return null or empty string as desired.

    public String getFirstElementName(){
String name = null;//or ""
if(myList.size() >0){
name = myList.get(0).getName);
}
return name;
}

EDIT:

    public A getFirstElement(){
A a = null;//or ""
if(myList.size() >0){
a= myList.get(0);
}
return a;
}

Where you are calling this method, you may write as:

   A a = getFirstElement();
String name = a.getName();
int number= a.getNum();

Hope this helps.

How to access specific element of array stored in arraylist?

use blow code:

double temp1[] = new double[alParent.size()];    
double temp2[] = new double[alParent.size()];
for (int i = 0; i < alParent.size(); i++) {
temp1[i] = alParent.get(i)[0];
temp2[i] = alParent.get(i)[1];
}

temp1 and temp2 are arrays you want.



Related Topics



Leave a reply



Submit