Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6578337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:45:29+00:00 2026-05-25T15:45:29+00:00

import java.util.AbstractList; public class ItemSet extends AbstractList { private Item[] arr; private ItemClass itemClass;

  • 0

import java.util.AbstractList;

public class ItemSet extends AbstractList {

private Item[] arr;
private ItemClass itemClass;

public ItemSet(Item item) {
arr = new Item[1];
arr[0] = item;
}

/*
 * (non-Javadoc)
 * 
 * @see java.util.AbstractList#add(java.lang.Object)
 */
@Override
public boolean add(Item e) {
boolean isNotAdded = true;
for (int i = 0; i < arr.length; i++) {
    if (e.getRule().compareTo(arr[i].getRule())) {
    if (e.getDot() == arr[i].getDot()) {
        isNotAdded = false;
        break;
    }
    }
}
if (isNotAdded) {
    Item[] oldArr = arr;
    arr = new Item[oldArr.length + 1];
    System.arraycopy(oldArr, 0, arr, 0, oldArr.length);
    arr[oldArr.length] = e;
}
return isNotAdded;
}

@Override
public Item get(int index) {
return arr[index];
}

@Override
public int size() {
return arr.length;
}

// SETTER

/**
 * @param itemClass
 *            the itemClass to set
 */
public void setItemClass(ItemClass itemClass) {
this.itemClass = itemClass;
}

//

// GETTER

/**
 * @return the itemClass
 */
public ItemClass getItemClass() {
return itemClass;
}
//

}

How to cover for instance removes methods?
How do not use an own list implementation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:45:30+00:00Added an answer on May 25, 2026 at 3:45 pm

    If you don’t want to inherit remove(), then don’t extend AbstractList, instead delegate the methods you want to an ArrayList.

    Like this (and for the love of god don’t comment your setters and getters)

        public class ItemSet {
        private final ArrayList<Item> items = new ArrayList<Item>();
        private ItemClass itemClass;
    
        public ItemSet(Item item) {
            items.add(item);
        }
    
        public boolean add(Item e) {
            boolean isNotAdded = true;
            for (Item item : items)
                if (e.getRule().compareTo(item.getRule()))
                    if (e.getDot() == item.getDot()) {
                        isNotAdded = false;
                        break;
                    }
            // why are you adding here?
            if (isNotAdded)
                items.add(e);
            return isNotAdded;
        }
    
        public Item get(int index) {
            return items.get(index);
        }
    
        public int size() {
            return items.size();
        }
    
        public void setItemClass(ItemClass itemClass) {
            this.itemClass = itemClass;
        }
    
        public ItemClass getItemClass() {
            return itemClass;
        }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; public class Main implements Runnable { private final CountDownLatch cdl1
import java.util.Scanner; public class Main extends Hashmap{ public static void main(String[] args) { Hashmap
import java.util.ArrayList; import java.util.Arrays; public class Cloud { private ArrayList<Point> points; private double left;
import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class LoginServlet extends HttpServlet {
import java.util.Scanner; public class Game { private String startYorN; private int generateTestValues; private int
import java.util.UUID public class Person implements Serializable { private UUID uuid; public Person() {
import java.util.*; public class Test { static List<Integer> list = new LinkedList<Integer>(); public static
import java.util.*; public class HashMapExample { public static class WriteOnceMap<K, V> extends HashMap<K, V>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.