[Solved] How can i sort objects in arrayList? [closed]

Use Comparable and Comparator as shown below, you can also visit https://www.journaldev.com/780/comparable-and-comparator-in-java-example for further details. import java.util.Comparator; class Employee implements Comparable<Employee> { private int id; private String name; private int age; private long salary; public int getId() { return id; } public String getName() { return name; } public int getAge() { return age; } … Read more

[Solved] How do I return/implement the toString for the ArrayList? Also, just want to check that I’ve correctly created my objects?

You have a lot of error in your code : First Don’t ever name your variables with Upper letter in beginning, java use camelCase Second Don’t declare your variable with private public in your method. private ArrayList<Object> List = new ArrayList<Object>(); Third You can’t declare a method inside another method you have to close the … Read more

[Solved] Remove duplicates from a Java List [duplicate]

There are two possbile solutions. The first one is to override the equals method. Simply add: public class DataRecord { […..] private String TUN; @Override public boolean equals(Object o) { if (o instanceof DataRecord) { DataRecord that = (DataRecord) o; return Objects.equals(this.TUN, that.TUN); } return false; } @Override public int hashCode() { return Objects.hashCode(TUN); } … Read more

[Solved] how to get substring items with in arraylist in java

you have two approaches: ArrayList<String> ar = new ArrayList(); ar.add(“UserId”); //adding item to arraylist ar.add(“Directory”); ar.add(“Username”); ar.add(“PhoneNumber”); // approach one using iteration for (Iterator<String> it = ar.iterator(); it.hasNext(); ) { String f = it.next(); if (f.equals(“UserId”)) System.out.println(“UserId found”); } // approach two using a colletion which supports fetching element by key Map<String,String> myMap=new HashMap<>(); for(String … Read more

[Solved] ERROR java.lang.ArrayIndexOutOfBoundsException: length=5; index=5

Exception line (java.lang.ArrayIndexOutOfBoundsException: length=5; index=5) clearly mentions that you are Trying to get index=5but the length of the dato is 5(length=5). So, use only proper index i.e. index 0 to 4. OR Make sure that enough indexes exists to access. Note: You have used dato.split(“, “);. Try with dato.split(“,”);. May be the problem is with … Read more

[Solved] Find String in ArrayList Containing Phrase

public static void main(String[] args) { List<String> data = new ArrayList<String>(); String yourRequiredString = “dessert”; data.add(“apple fruit”); data.add(“carrot vegetable”); data.add(“cake dessert”); for (int i = 0; i < data.size(); i++) { if (data.get(i).contains(yourRequiredString)) { System.out.println(i); } } } Hope this helps… solved Find String in ArrayList Containing Phrase

[Solved] How to make ArrayList of hashset in java? [closed]

Just make an ArrayList of HashSets : ArrayList<HashSet<Integer>> list = new ArrayList<HashSet<Integer>>(); Then create HashSets, fill them, and put them in your ArrayList normally. HashSet<Integer> set = new HashSet<Integer>(); set.add(1); set.add(whateverIntValue); list.add(set); You can then get the nth HashSet of your list using list.get(n). solved How to make ArrayList of hashset in java? [closed]

[Solved] What is different between while(aPointer) and a if(aPointer) in C++? [closed]

In the first example, the loop runs until the pointer itself becomes null, which it never does (instead, it’s eventually incremented past the end of the buffer, whereupon the program exhibits undefined behavior). In the second example, the loop runs until the character pointed to becomes zero – which it does once the advancing pointer … Read more

[Solved] When I try to add Strings to an Array List I get an “illegal start of type error”

To add items to a static list you can use the block static{} public class Employee { …. private static ArrayList <String> employeeID = new ArrayList <String> (); static { employeeID.add(“632584”); employeeID.add(“259415”); employeeID.add(“257412”); employeeID.add(“953647”); employeeID.add(“126497”); employeeID.add(“453256”); employeeID.add(“125689”); } … } } Or use constructor public class Employee { …. private static ArrayList <String> employeeID = … Read more