[Solved] Arraylist java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3 Error

The length of the array should change when you remove elements. The arrayLen variable stores the length of the array, but doesn’t change when the array’s length shrinks. To change it, you should be able to just replace arrayLen with arrayList.size(), which will change when your remove elements 0 solved Arraylist java.lang.IndexOutOfBoundsException: Index 3 out … Read more

[Solved] duplicates from the ArrayList

Answer explained in comments import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.commons.io.FileUtils; public class Practice { public static void main (String[] args) { // ArrayList<String> array = new ArrayList<String>(); // ArrayList<String> dup = new ArrayList<String>(); File file = new File (“christmas_carol.txt”); try { … Read more

[Solved] HashMap of ArrayList as the value initialization

The List<Pack> in your HashMaps are never defined. They are of the interface list but nowhere you identify what kind of List they are. You have to follow a structure as follows: Map<long, List<Pack>> map = new HashMap<>() // java 7 syntax List<Pack> packets = new ArrayList<>(); packets.add(pack); map.put(msg.getAck(), packets); When adding an element to … Read more

[Solved] How to fix ClassCastException in enhanced for-loop? [closed]

The only way the problem can exist given the posted code, if the reported exception/line is correct, is when “Somerecords” is not really creating a List<Object[]> object – but a List containing non-Object[] elements. One reason could be the method in question is typed to return a non-generic List, filled with double values. Java will … Read more

[Solved] Removing duplicates from arraylist using set

Find the intersection Find the union Subtract the intersection from the union Code: public static void main(String[] args) { Set<Integer> set1 = new HashSet<Integer>(Arrays.asList(1, 2, 3, 4, 5)); Set<Integer> set2 = new HashSet<Integer>(Arrays.asList(1, 3, 6, 7)); Set<Integer> intersection = new HashSet<Integer>(set1); intersection.retainAll(set2); // set1 is now the union of set1 and set2 set1.addAll(set2); // set1 … Read more

[Solved] I am new in using ArrayLists. Array Out of Bounds, Array seems to be empty, but have been adding Strings to it already

First of all, and that’s supposed to solve the problem. If you use an ArrayList you should check its documentation. When you supposed to go through a simple array you use “nameOfArray.length”, right? Well, the ArrayList has the method “.size()” which bascialy has the same use. So your loop would be better this way : … Read more

[Solved] why is it that we can’t use ArrayList.get(-1) to get the last element of the ArrayList in java? it works in python though [closed]

First, Java is not Python (although Jython implements Python in Java). Second, you should read the JavaDoc on ArrayList – that is it throws an Exception, IndexOutOfBoundsException – if the index is out of range (index < 0 || index >= size()) Finally, you can do this myList.get(myList.size() – 1); to get this last element … Read more

[Solved] Undesired output from foreach loop and IF statement

Add DisplayType = cmdType.SelectedIndex to the AddEntry Tried using a String Builder? StringBuilder sb = new StringBuilder(mainWindow.ChangeTextBox); foreach (AddEntry list in addedEntry) { sb.AppendLine(list.Type); if (list.DisplayType == 1) sb.AppendLine(“URL: ” + list.URL); if (list.DisplayType == 0 || list.DisplayType == 1) { sb.AppendLine(“User Name: ” + list.UserName); sb.AppendLine(“Password: ” + list.Password); } if (list.DisplayType == 2) … Read more

[Solved] using onClick to open details depending on the position of Recycler view

Create Constructor in adapter class : public DataAdapter(List<Pojo> dataList, OnItemClickListener listener) { this.dataList = dataList; this.listener = listener; } Create OnBindViewHolder() method and get position : @Override public void onBindViewHolder(MyViewHolder holder, int position) { final Pojo movie = dataList.get(position); holder.Showid.setText(movie.getCatagory_id()); holder.fname.setText(movie.getCatagory_name()); holder.thumbNail.setImageUrl(movie.getCatagory_thumbnailUrl(), imageLoader); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { listener.onItemClick(movie.getSubCatagoryArrayList()); } }); … Read more

[Solved] How to keep only the object with the maximum value in the ArrayList?

This should be working. List<Counter> toRemove = new ArrayList<Counter>(); Collections.sort(interval, (first, second) -> { int c = 0; if (first.compareWith(second)) { if (first.getCount() <= second.getCount()()) toRemove.add(first); else if (first.getCount() >= second.getCount()) toRemove.add(second); } else c = first.getCount().compareTo(second.getCount()); return c; } ); interval.removeAll(toRemove); This is the compareWith function in the Counter class. public boolean compareWith(Counter second) … Read more