I have a class Section with several methods including methods get_key() and get_angle(). Items of type Section are added to a hashtable implemented in class Hashtable.
According to my task I should delete such elements from the hashtable which have bigger value of function get_angle() than given_value.
class Hashtable{
private Section[] hash_array; //array of cells of the hashtable
public int size;
public void remove_given(double given_value)
{
for(int i = 0; i < size; i++)
{
if (hash_array[i] != null)
{
double value = hash_array[i].get_angle(); //value of needed function to compare
if (value > given_value)
{
int key_ = hash_array[i].get_key(); //get key for the item in order to delete it
Delete(key_); //delete item
}
}
}
}
}
But the method doesn`t delete any elements. I checked the method Delete() separately and it works just fine as well as other methods called on this method . I really need to figure it out. So I will be grateful for your help.
Debug your code, does it enter the for-loop. How do you initialize the value of size variable? If you forget to initialize it by default it will be zero. It is better to get the size from the hash_array.length.
For one thing you're using the uninitialized global var, size, the size used in the for loop needs to be the size of the Hash collection. Also how is the Hash initialized? Does it contain what you think? I'd follow the aforementioned suggestion to step through the code with a debugger, perhaps the keys aren't what you think they are...
Related
My Problem
I have a fixed-size ArrayList which contains custom variables. Despite of the ArrayList having a fixed size, sometimes a lot of them will actually be null. The thing is that I need to return the ArrayList without the null variables inside it. One important thing to note: the ArrayList will have all of its non-null items first, and then all of the nulls below them, e.g., the elements are not mixed. Example: [non-null, non-null, .... null, null, null]
My workaround
I though of creating a for-loop that checked (from last to first index) each of the elements inside the ArrayList to determine if it's null or not. If is null, then I'd call this code:
for (i = size-1; i >=0 ; i--) {
groupList = new ArrayList<>(groupList.subList(0, i));
}
My question
If the ArrayList is too big, this method might me particularly slow (or not?). I was wondering if there exists a better, more performance-friendly solution. AFAIK the .subList method is expensive.
You can have a variant of binary search, where your custom comparator is:
Both elements are null/not null? They are equal
Only one element is null? The none null is "smaller".
You are looking for the first null element.
This will take O(logn) time, where n is the size of the array.
However, taking the sublist of the ArrayList that is none null (assuming you are going to copy it to a new list object), is going to be linear time of the elements copied, since you must "touch" each of them.
This gives you total time complexity of O(logn + k), where k is number of non null elements, and n is the size of the array.
Following all of your outstanding advices, I modified the original method so that I can take the last (first) ever null item position and call the .subList method just once. And here it is:
int lastNullIndex = size - 1;
for (i = lastNullIndex; i >= 0; i--) {
if (null == groupList.get(i)) {
lastNullIndex = i;
} else {
break;
}
}
groupList = new ArrayList<>(groupList.subList(0, lastNullIndex));
return groupList;
If you think it can be further modified so as to allow for a better performance, let us know.
I am iterating through an array of objects (different website types). But I want to be able to show which website has the highest amount of members. Initially my code shows the amount of members of each website, but my code isn't complete as im not sure how to compare the objects to see which has the highest amount of members. Current code:
public static void mostUsers(){
for (Website w: websiteTypes){
// check if the array index is not null
if (w!=null){
System.out.println(w.getType() + "has:" + w.getMembers());
System.out.println(); // line break
**//Code to compare amount of users on each site and display highest**
} else {
// no more records so quit loop
break;
}
}
}
You need to keep track of the maximum number of users while your iterating.
Initialize a variable called maxWebsite to null before your loop
Initialize an integer called max to Integer.MIN_VALUE before your loop
While your iterating, test if w.getMaxUsers() is superior than the max value.
If it is, update accordingly the maxWebsite and max variables
Note : remove the else statement in your loop, you have to iterate all the elements of your array.
If you're using Collections, that could be simple using Collections.max with a custom comparator :
List<Website> l = .......
l.removeAll(Collections.singleton(null)); //remove null elements from the list
Website maxWebsite = Collections.max(l, new Comparator<Website>(){
#Override
public int compare(Website arg0, Website arg1) {
return Integer.compare(arg0.getMaxUsers(), arg1.getMaxUsers());
}
});
Use a Comparator, or have Website extend Comparable.
Then, sort your collection of Websites using sort. The websites with the most views will then be at the beginning of your collection.
The following code should be returning 16 as far as I can tell but for some reason, it returns 10. Does anyone know what my bug might be? Basically it's the Knapsack problem in Java and I've ran through the whole code on paper and it seems to return the right answer to me but I cannot figure out why when it's properly run, it returns back 10.
Any help would be much appreciated. Thanks!
import java.util.Stack;
public class knapsackProblem
{
public static int optimalValue(Stack<item> items, int totalWeight)
{
if (items.isEmpty())
return 0;
int value = items.peek().value;
int weight = items.peek().weight;
items.pop();
if (totalWeight<weight)
return optimalValue(items, totalWeight);
return Math.max(optimalValue(items,totalWeight), value + optimalValue(items, totalWeight-weight));
}
public static void main(String args[])
{
int knapsackWeight = 15;
Stack<item> items = new Stack<item>();
items.push(new item(7,10));
items.push(new item(3,6));
System.out.println(optimalValue(items, knapsackWeight));
}
}
class item
{
public int weight;
public int value;
public item(int aWeight, int aValue)
{
weight = aWeight;
value = aValue;
}
}
Your Stack is being modified across the calls. So a line like
return Math.max(optimalValue(items,totalWeight), value + optimalValue(items, totalWeight-weight));
will have two different copies of items for each call. Not what you want.
Instead of using Stack, try changing things around to use an ArrayList. Then pass your index of which item you're evaluating into the optimalValue method instead. This should help you work through the items correctly.
I haven't gone through the whole algorithm, but an obvious problem is that every time you call optimalValue on a Stack, it will pop one or more items from the stack. But a Stack, and the items in the stack, are objects, which means they're passed around by reference. So in this line:
return Math.max(optimalValue(items,totalWeight), value + optimalValue(items, totalWeight-weight));
This calls optimalValue twice. The first time you call it with items as a parameter, optimalValue will pop one or more elements from items. Then the statement calls optimalValue again with items as a parameter--and this will NOT use the same items stack that you passed to the first optimalValue call, but it will use the items with the already-popped-off items still popped off (from the first call). I really doubt this is what you want. If you do things this way, then at some point I think you'll have to make a copy of your Stack. Or you'll need to rethink things and do it a different way (maybe you can use an array or ArrayList, so that the items aren't actually popped off but you could pass a "starting index" from one optimalValue call to the recursive call).
I don't know whether there are other problems with your solution in addition to this one.
I have a custom object in this structure
static class Node {
int col;
int row;
int g;
int h;
int f;
public Node(int col, int row, int g, int h) {
this.col = col;
this.row = row;
this.g = g;
this.h = h;
this.f = g+h;
}
}
The col and row variables are unique, and may only occur once in ArrayList<Node> myList.
Is there a way optimal way to avoid adding or checking for possible duplicate without having to make a nasty for-loop?
I am aware that Set interface possibly could be a solution for this as duplicates cannot occur, but i have alot of code right now, which i do not want to refactor unless it becomes necessary.
Here are your options. All of these solutions require proper implementation of equals and hashCode. Since you want row and col to be unique:
public boolean equals(Object obj) {
if (obj == null || obj.getClass() != Node.class) {
return false;
}
Node other = (Node) obj;
if (other.col != this.col) {
return false;
}
if (other.row != this.row) {
return false;
}
return true;
}
public int hashCode() {
int result = 7;
result += row * 31;
result += col * 31;
return result;
}
Iterate over the List
You don't have to do the iteration yourself, but that is exactly what calling List.contains will do. This one is pretty easy:
if (!myList.contains(node)) {
myList.add(node);
}
This will iterate for you, so you don't have to write the loop.
List to Set to List
Here you have two sub-options. If you want to preserve the order of your input list, then you can use LinkedHashSet. If you don't care, you can just use HashSet. What I mean is if I have a List with elements A, B, C, converting it to a HashSet and back may produce a different list, like B, C, A. LinkedHashSet keeps the elements in insertion order, avoiding this problem. In any case, you'll just do this:
Set<Node> nodeSet = new [Linked]HashSet<Node>(myList);
nodeSet.add(node);
myList = new ArrayList<Node>(nodeSet);
Remember that this is essentially doing iteration as well, but it's using a hash-code shortcut instead of checking every element's equality, which may be a big deal with enough nodes. If your node list is small (less than 1000 elements) then I doubt this will make much of a difference, and you may as well use the first one.
Converting everything to Set
You mentioned that this would require a lot of refactoring in your code, but this isn't a bad thing, especially if you plan on working on this code a lot in the future. My rule of thumb is if the refactoring will make the code easier to maintain, adding a little extra development time is never a bad thing. Writing maintainable, readable, and understandable code is what the experts do (the question here isn't relevant, but this particular answer is). Since Set implies unique elements and List does not, then it makes sense to make the change. The compiler will pretty much tell you all the places you have to change with its errors, and it might take less time than you think.
Add a equals method in Node if possible :
#Override
public boolean equals(Node n){
if(this.getRow().equals(n.getRow()) && this.getCol().equals(n.getCol())
return true;
else
return false;
}
And then use list-to-set-to-list trick.
Try this method:
List<Node> removeDuplicateNodes(List<Node> inputList){
return (inputList ==null or inputList.size()==0)? Collections.EMPTY_LIST: new ArrayList<Node>(new HashSet<Node>(inputList));
}
Update :
If you wan't to maintain input order, use LinkedHashSet
List<Node> removeDuplicateNodes(List<Node> inputList){
return (inputList ==null or inputList.size()==0)? Collections.EMPTY_LIST: new ArrayList<Node>(new LinkedHashSet<Node>(inputList));
}
Add all the elements to a new Set, then put all the elements from the Set to a new List. That will make it.
I always find weird to see cases where people want to use a List (for the get(int) method, I guess) when they require unicity, which is only achieved through Set.
Anyway, by manipulating a little the equals/hashcode (making equals return true when row and col are the same) method and adding calls to List#contains(Object), you could have your goal reached without sacrifying your List
EDIT
Notice you could also create a Comparator and rely upon Collections#sort(List, Comparator) to have your list sorted and items with the same value melted into only one value.
Keep both a Set and a List. Use the Set to check for duplicates. Add to both Set and List if no dupe.
...assuming that Node has an .equals method defined...
private final Set<Node> seen = new HashMap<Node>();
private final List<Node> uniqueNodes = new ArrayList<Node>();
public void insertIfUnique(final Node n) {
if (seen.contains(n)) {
return;
}
seen.add(n);
uniqueNodes.add(n);
}
Ideally, you'd use Set, but if you'd like to avoid reimplementing your data structures from ArrayList<Node> to Set, you can implement Set as a gatekeeper:
Each time an element is about to be inserted into your ArrayList, check if the row-col pair is already in the Set.
If not, register the row-col pair into the Set
If the pair already exists in the set, do not insert it
Each time an element is about to be removed from your ArrayList, remove it from the Set.
And thus it's a "gatekeeper".
All of the Set operations are O(1) since they are hashed; minimal refactoring and no nasty loops as desired.
I have a problem where I retrieve and element from a list (list1), and modify one of the parameters in the element and then add it to another list (list2). When I do this to the final item in list1, it will sometimes modify the parameters of the elements in list2.
This function is called once per generation, but it is not until about the 8th generation when I start to see this happen. It has affect anywhere from 2 to 16 elements in list2.
Any ideas where my screw up might be? Here's a block of code I wrote to illustrate the problem. The problem occurs in the section where I check count==0.
public void sampleCode (List list) {
List differentList = new ArrayList();
Individual element;
Individual differentelement;
int i = 0;
int count = 0;
for(i = 0; i < list.size(); i++) {
element = (Individual) list.get(i) ;
// does some checking to see if this meets criteria
// this is sorta pseudo code
if(probability == true) {
element.setDoMutation(true);
count++;
}
//always add this element to differentList
//even if no changes are made to the element
differentList.add(i,element);
}
//need to make sure one elements has mutation=true;
if(count == 0) {
differentelement = (Individual) list.get((list.size()-1));
//setting this element field changes the contents of
//different list.
differentelement.setDoMutation(true);
differentList.set((list.size()-1), differentelement);
}
}
In Java, a variable doesn't hold an object. It holds an object reference (i.e. a pointer to an object). Getting an object fom a list and putting it in another list doesn't make a copy of the object. Both lists simply have a pointer to the same object. So, of course, if you modify the contents of the object, both lists will have the object modified.
Side note: You should use parameterized types (i.e. List<Individual> rather than List), and avoid declaring variables at the beginning of your methods as you would do in C. Only declare a variable when you need it. This will make the code much clearer, and reduce the scope of your variables.
Are you sure that the block
//need to make sure one elements has mutation=true;
if(count == 0) {
differentelement = (Individual) list.get((list.size()-1));
//setting this element field changes the contents of
//different list.
differentelement.setDoMutation(true);
differentList.set((list.size()-1), differentelement);
}
has to be inside the loop
for(i = 0; i < list.size(); i++) {
...
}
I suspect it has to be moved after the loop in order to work correctly.