how can I sort an Array after deleting one value?
e.g. {1,2,3,4,5} --> {1,2,null,4,5} --> {1,2,4,5,null}.
public boolean removeNumber(int accountNumber) {
for(int i = 0; i < Account.length; i++) {
if(Account[i].getAccountNumber() == (accountNumber)) {
Account[i] = null;
}
Since your original array is already sorted, and assuming that only one Account can have that particular accountNumber, there is no need to sort the array again. Just locate the element to remove, then shift everything after that element down one position, and null out the newly-freed element.
I've renamed the Account field to accounts to follow Java naming conventions, to make a clearer distinction between the array (accounts) and the class (Account), both for the future developers looking at the code, and for the Java compiler.
public boolean removeNumber(int accountNumber) {
for (int i = 0; i < accounts.length; i++) {
if (accounts[i].getAccountNumber() == accountNumber) {
System.arraycopy(accounts, i + 1, accounts, i, accounts.length - i - 1);
accounts[accounts.length - 1] = null;
return true; // Found and removed
}
}
return false; // Not found, so nothing removed
}
Now, if you insist on sorting the array, you can do it like this in Java 8+:
Arrays.sort(accounts, Comparator.nullsLast(Comparator.comparing(Account::getAccountNumber)));
You can follow this way to achieve this :
You need to find the accounts that have the accountNumber you're looking for
you set them to null on the array
You sort the array with a custom Comparator :
if an element is null it has to go at the end
if both are not-null you compare their accountNumber
public boolean removeNumber(int accountNumber) {
for (int i = 0; i < Account.length; i++) {
if (Account[i].getAccountNumber() == accountNumber) {
Account[i] = null;
}
}
Arrays.sort(Account, (o, p) -> {
if (o == null)
return 1;
if (p == null)
return -1;
return Integer.compare(o.getAccountNumber(), p.getAccountNumber());
});
return true;
}
Tips :
follow naming conventions : camelCasefor attriutes ==> Account[] becomes account[]
use .equals() when you are comparing object, for an int == is right
Related
So, I am in a Data Structures class and we are writing code and varying methods for Hashing. I am actually having trouble with the "get" method. The tests we have run fine until the last "key9" which is asserted to return null. The for loop for some reasons exits and the keyStartIndex is instantiated again. The method is not recursive so I have no idea why this is transpiring. Code is below. Any help is greatly appreciated.
Method I am trying to complete, that is having issues.
...
public String get(String key) {
//TODO : complete the method
int keyStartIndex = (int) hashFunction(key) % items.length;
for(int i = keyStartIndex; i < items.length; i++){
if(items[i].key == hashFunction(key)){
return items[i].item;
} else if(i == items.length-1){
i=0;
continue;
}
}
return null;
}
...
All prior code in this class that applies to this method
...
import java.util.Arrays;
import jdk.internal.org.objectweb.asm.tree.analysis.Value;
class DataItem {
long key;
String item;
public DataItem(long key, String item) {
this.key = key;
this.item = item;
}
#Override
public String toString() {
return String.format("{%s:%s}", key, item);
}
}
public class HashMap {
private int size = 0;
private static final int INITIAL_SIZE = 10;
private static final int DELETED_KEY = 0;
private DataItem[] items;
public HashMap() {
items = new DataItem[INITIAL_SIZE];
}
public int size() {
return size;
}
public long hashFunction(String key) {
long hashed = 0;
for(int i = 0; i < key.length(); i++){
hashed += key.charAt(i)*(Math.pow(27, i));
}
return hashed;
}
public void put(String key, String value) throws TableIsFullException {
if (size >= items.length-1){
throw new TableIsFullException();
} else {
DataItem input = new DataItem(hashFunction(key), value);
for(int i = ((int) input.key % items.length); i < items.length; i++){
if(items[i] != null){
continue;
}else if(i == items.length - 1 && items[i] != null){
i = 0;
continue;
} else {
items[i] = input;
size++;
break;
}
}
}
}
...
----------------------------------------------And the tests that are being ran, only the last one is failing again, with "key9". I have ran debugger and it says there is a nullPointerException. Again, with break points, for some reason it leaves the for loop and processes another key, key3 to be specific. I have no idea why this is happening.
#Test
public void testGet() throws TableIsFullException {
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
map.put("key4", "value4");
map.put("key5", "value5");
map.put("key6", "value6");
assertEquals("value3", map.get("key3"));
assertEquals(null, map.get("key9"));
}
Your put and get methods don't implement wraparound correctly. That means that when you get a couple of hash collisions towards the end of the table, things go haywire.
Secondly, you are not handling hash collisions correctly in either method. The contract for hash is that obj1.equals(obj2) implies hash1 == hash2, but not the other way around. That means that DataItem must record the original object as well as the hash. I will assume that you've added the appropriate field, and that DataItem now has three fields names key, hash and value.
Let's start with put:
The end condition is i >= items.length, so you either have an infinite loop if you wrap around, or you never wrap around.
Since you check items[i] != null first, i == items.length - 1 && items[i] != null can never be triggered, and so you never wrap around when the end of the table is full.
You never check if the existing item matches the new key.
One way to correct put is too treat items as a circular buffer. That means that you subtract off an offset modulo items.length:
int hash = hashFunction(key);
int offset = hash % items.length);
for(int i = 0; i < items.length; i++) {
int k = (i + offset) % items.length;
if(items[k] == null) {
items[k] = new DataItem(key, hash, value);
size++;
break;
}
if(items[k].hash == hash && items[k].key.equals(key)) {
items[k].value = value;
break;
}
}
You also need to fix your size check before throwing an exception. Checking size >= items.length - 1 will throw an exception when there is one free slot available. The correct condition is
if(size >= items.length) {
Your get method suffers from the same issue with wraparound as put. It also has the problem that you're checking for hash equality and not object equality when you retrieve an object.
int hash = hashFunction(key);
int offset = hash % items.length;
for(int i = 0; i < items.length; i++) {
int k = (i + offset) % items.length
if(items[k].hash == hash && items[k].key.equals(key)){
return items[i].value;
}
}
return null;
The check items[k].item.equals(key) is critical for resolving hash collisions correctly. Notice that it is only performed when the hashes match because of short circuiting.
Try to avoid recomputing values like hash inside a loop.
This whole scheme breaks down if you try to support a remove operation. If you notice, put will stop searching for matches once it finds an empty slot. This will break down if you can create empty slots before the matching object.
The NullPointerException that you see occurs because in the get() method you write
if(items[i].key == hashFunction(key))
Now if the specific key has not been added to the HashMap (and the items array is not yet full) the entry items[i] is still null and trying to access items[i].key gives the NullPointerException that you see.
The shortest test case for leads to your problem is:
#Test
public void keyNotFound() throws TableIsFullException {
assertEquals(null, map.get("key9"));
}
Besides that, carefully read the answer of "Mad Physicist" because it addresses other design flaws in your implementation (although not this one).
I have a set of sets of integers: Set<Set<Integer>>.
I need to add integers to the set of sets as if it were a double array. So add(2,3) would have to add integer 3 to the 2nd set.
I know a set is not very suitable for this operation but it's not my call.
The commented line below clearly does not work but it shows the intention.
My question is how to add an integer to a set while iterating?
If it's necessary to identify each set, how would one do this?
#Override
public void add(int a, int b) {
if (!isValidPair(a, b)) {
throw new IllegalStateException("!isValidPair does not hold for (a,b)");
}
Iterator<Set<Integer>> it = relation.iterator();
int i = 0;
while (it.hasNext() && i <= a) {
//it.next().add(b);
i++;
}
}
One fundamental things you should be aware of, for which makes all existing answer in this question not working:
Once an object is added in a Set (similarly, as key in Map), it is not supposed to change (at least not in aspects that will change its equals() and hashCode()). The "Uniqueness" checking is done only when you add the object into the Set.
For example
Set<Set<Integer>> bigSet = new HashSet<>();
Set<Integer> v1 = new HashSet<>(Arrays.asList(1,2));
bigSet.add(v1);
System.out.println("contains " + bigSet.contains(new HashSet<>(Arrays.asList(1,2)))); // True
v1.add(3);
System.out.println("contains " + bigSet.contains(new HashSet<>(Arrays.asList(1,2)))); // False!!
System.out.println("contains " + bigSet.contains(new HashSet<>(Arrays.asList(1,2,3)))); // False!!
You can see how the set is corrupted. It contains a [1,2,3] but contains() does not work, neither for [1,2] nor [1,2,3].
Another fundamental thing is, your so-called '2nd set' may not make sense. Set implementation like HashSet maintain the values in arbitrary order.
So, with these in mind, what you may do is:
First find the n-th value, and remove it
add the value into the removed value set
re-add the value set.
Something like this (pseudo code again):
int i = 0;
Set<Integer> setToAdd = null;
for (Iterator itr = bigSet.iterator; itr.hasNext(); ++i) {
Set<Integer> s = itr.next();
if (i == inputIndex) {
// remove the set first
itr.remove();
setToAdd = s;
break;
}
}
// update the set and re-add it back
if (setToAdd != null) {
setToAdd.add(inputNumber);
bigSet.add(setToAdd);
}
Use a for-each loop and make your life easier.
public boolean add(int index, int value) {
// because a and b suck as variable names
if (index < 0 || index >= values.size()) {
return false;
}
int iter = 0;
for (Set<Integer> values : relation) {
if (iter++ == index) {
return values.add(value):
}
}
return false;
}
Now all you have to figure out is what to do if relation is unordered, as a Set or a relation are, because in that case a different Set<Integer> could match the same index each time the loop executes.
Use can use Iterators of Guava library like this :
#Override
public void add(int a, int b) {
if (!isValidPair(a, b)) {
throw new IllegalStateException("!isValidPair does not hold for (a,b)");
}
Iterators.get(relation.iterator(), a).add(b);
}
Edit : without Guava:
Iterator<Set<Integer>> iterator = relation.iterator();
for(int i = 0; i < a && iterator.hasNext(); ++i) {
iterator.next();
}
if(iterator.hasNext()) {
iterator.next().add(b);
}
For a programming assignment, I have been working on creating a program that reads an input file and sorts the data inside using a self-made max heap priority queue. The data file has lines that either read "insert [a name] [a number]", or "remove". For this priority queue, we need to make a function for inserting and removing objects. Each object in the queue contains the name as a string, and the priority as a integer. I have to implement this heap based on an array with a size of 255.
My question is, I'm having difficulty implementing my insert and remove functions to work as specified. I'll provide 1) how they need to work, 2) pseudocode I've made, and 3) the actual Java code I've implemented. Both of my functions do not work exactly as I intend for them to, so I could use some direction from more experienced programmers.
1) insert(name, priority): this function should take a name of type string and a priority of type integer, and inserts them into the priority queue.
remove(): this function should remove the object with the highest priority value and return the name string from the object.
2) As background, I have three classes for this program: First, the "main" class containing implementation for reading the file and using the functions. Second, the "name" class, which creates the name object containing the name string and priority int , a constructor, and a compareTo method for comparing the priority values of two objects. Third, the "priorityqueue" class, contains the insert and remove functions. Now, here is the pseudocode I made for those two functions:
insert:
Check if the array is full (when num = 255), throw if true
Create the object from the input file with a name string and priority int
Insert the object at num
Use a while loop to swap the two objects at insertion
Update num (num++)
remove:
Save the first object
Move the last object to the first
Update num (num--)
Use a while loop to determine the larger child and return it.
3) Here is the code I have so far. I'll provide my name and priority queue classes, in case my name class is what's giving me trouble.
Priority Queue class:
public class PriorityQueue
{
int num; //amount of things in array
int idx; //index of current name object
Name[] names = new Name[255];
public void insert(String name, int priority)
{
while (num != 255)
{
Name addName = new Name(name, priority);
names[num] = addName;
num++;
while(idx != 0 || names[idx].CompareTo(names[(idx-1)/2]))
{
Name temp = names[idx];
names[idx] = names[(idx-1)/2];
names[(idx-1)/2] = temp;
idx = (idx-1)/2;
}
}
}
public Name remove()
{
Name temp2 = names[0];
//Save first element
names[0] = names[idx];
//Move last element to first
num--;
while(idx < Math.max(2*idx+1,2*idx+2))
{
if(names[idx].CompareTo(names[(idx-1)/2]))
{
Name temp3 = names[idx];
names[idx] = names[(idx-1)/2];
names[(idx-1)/2] = temp3;
}
}
return temp2;
}
}
Name class:
public class Name implements Comparable
{
String name;
int priority;
public Name(String n, int i)
{
name = n;
priority = i;
}
public boolean CompareTo(Name obj)
{
if(priority < obj.priority)
{
return false;
}
else if(priority > obj.priority)
{
return true;
}
return true;
}
}
I appreciate any help. Thanks!
Several problems. First, in your insert method:
public void insert(String name, int priority)
{
while (num != 255)
{
Name addName = new Name(name, priority);
names[num] = addName;
num++;
while(idx != 0 || names[idx].CompareTo(names[(idx-1)/2]))
{
Name temp = names[idx];
names[idx] = names[(idx-1)/2];
names[(idx-1)/2] = temp;
idx = (idx-1)/2;
}
}
}
The while (num != 255) shouldn't be there. You should check to see if num == 255, and throw an exception if it is.
Then, you need to initialize idx. That is:
Name addName = new Name(name, priority);
names[num] = addName;
idx = num;
num++;
And your while condition should use && rather than ||. Otherwise you'll do the swap every time idx is not equal to 0.
In your remove method:
public Name remove()
{
Name temp2 = names[0];
//Save first element
names[0] = names[idx];
//Move last element to first
num--;
while(idx < Math.max(2*idx+1,2*idx+2))
{
if(names[idx].CompareTo(names[(idx-1)/2]) > 0)
{
Name temp3 = names[idx];
names[idx] = names[(idx-1)/2];
names[(idx-1)/2] = temp3;
}
}
return temp2;
}
You don't want names[idx] there, because you don't know what idx is. You want:
names[0] = names[num-1]; // get last item in the heap
Your while condition here is goofy. Math.max(2*idx+1,2*idx+2) will always return 2*idx+2, unless idx is negative. And, again, you haven't even initialized idx. What you want here is:
idx = 0;
while (idx < num)
Now, what you're trying to do is see if names[idx] is smaller than either of its children. And, if so, select the largest of the two children to swap it with. So:
while (idx < num)
{
int largestChild = idx*2+1;
if (largestChild >= num) break; // idx is at a leaf level
if (largestChild+1 < num)
{
// compare the two children
if (names[largestChild].compareTo(names[largestChild+1]) < 0)
{
largestChild = largestChild+1;
}
}
if (names[idx] < names[largestChild])
{
// swap, moving the item down
temp = names[idx];
names[idx] = names[largestChild];
names[largestChild] = temp;
idx = largestChild;
}
else
{
// item is in the proper place
break;
}
}
I would suggest making idx a method-scoped variable in both methods. There's no need for it to be a global, and making it local to the methods forces you to initialize it before you use it, rather than potentially (as in your existing code) using a stale value.
I think you need to change your Name class's CompareTo function. The Comparable compareTo function must return:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
So you should have:
public boolean CompareTo(Name obj)
{
if(priority < obj.priority)
{
return -1;
}
if (priority > obj.priority)
{
return 1;
}
return 0;
}
Imagine the following object
class Trip {
String name;
int numOfTravellers;
DateMidnight from;
DateMidnight too;
}
I have written a manual recursive filter and transform method in java. However, I think this could be written more eloquently using Google Guava.
Can someone help me out and tell me how I can rewrite this to make more readable?
Basically what this method does, is locating equal entries, and combining the ones that are equal by altering the date fields
List<Trip> combineEqual(List<Trip> list) {
int n = list.size() - 1;
for (int i = n; i >= 0; i--) {
for (int j = n; j >= 0; j--) {
if (i == j) {
continue;
}
if (shouldCombineEqual(list.get(i), list.get(j))) {
Trip combined = combine(list.get(i), list.get(j));
list.remove(i);
list.remove(j);
list.add(Math.min(i, j), combined);
return combineEqual(liste);
}
}
}
return list;
}
private boolean shouldCombineEqual(Trip a, Trip b) {
return shouldCombineWith(a, b) || shouldCombineWith(b, a);
}
private boolean shouldCombineWith(Trip a, Trip b) {
return a.too() != null
&& a.too().plusDays(1).equals(b.from)
&& areEqual(a, b);
}
private boolean areEqual(Trip a, Trip b) {
return equal(a.name,b.name) && equal(a.numOfTravellers, b.numOfTravellers);
}
private boolean equal(Object a, Object b) {
return a == null && b == null || a != null && a.equals(b);
}
private Trip combineEqual(Trip a, Trip b) {
Trip copy = copy(a); //Just a copy method
if (a.from.isAfter(b.from)) {
Trip tmp = a;
a = b;
b = tmp;
} // a is now the one with the earliest too date
copy.from = a.from;
copy.too = b.too;
return copy;
}
I don't think Guava can help much here. There's a lot you can improve without it:
Create a TripKey {String name; int numOfTravellers;}, define equals, and use it instead of your misnamed areEqual. Split your trips into lists by their keys - here ListMultimap<TripKey, Trip> can help.
For each key, sort the corresponding list according to from. Try to combine each trip with all following trips. If it gets combined, restart the inner loop only. This should be already much clearer (and faster) than your solution... so I stop here.
I'd just use a HashSet.
First define equals and hashcode in your trip object. Add the first list to the set. Then iterate through the second list checking if a matching trip is already in the set. Something like:
public static Set<Trip> combineEquals(List<Trip> 11, List<Trip> 12) {
Set<Trip> trips = new HashSet<>(11);
for ( Trip t: 12) {
if ( trips.contains(t)) {
// combine whats in the set with t
} else {
trips.add(t);
}
}
return trips;
I have an integer arraylist..
ArrayList <Integer> portList = new ArrayList();
I need to check if a specific integer has already been entered twice. Is this possible in Java?
You could use something like this to see how many times a specific value is there:
System.out.println(Collections.frequency(portList, 1));
// There can be whatever Integer, and I use 1, so you can understand
And to check if a specific value is there more than once you could use something like this:
if ( (Collections.frequency(portList, x)) > 1 ){
System.out.println(x + " is in portList more than once ");
}
My solution
public static boolean moreThanOnce(ArrayList<Integer> list, int searched)
{
int numCount = 0;
for (int thisNum : list) {
if (thisNum == searched)
numCount++;
}
return numCount > 1;
}
If you are looking to do this in one method, then no. However, you could do it in two steps if you need to simply find out if it exists at least more than once in the List. You could do
int first = list.indexOf(object)
int second = list.lastIndexOf(object)
// Don't forget to also check to see if either are -1, the value does not exist at all.
if (first == second) {
// No Duplicates of object appear in the list
} else {
// Duplicate exists
}
This will tell you if you have at least two same values in your ArrayList:
int first = portList.indexOf(someIntValue);
int last = portList.lastIndexOf(someIntValue);
if (first != -1 && first != last) {
// someIntValue exists more than once in the list (not sure how many times though)
}
If you really want to know how many duplicates of a given value you have, you need to iterate through the entire array. Something like this:
/**
* Will return a list of all indexes where the given value
* exists in the given array. The list will be empty if the
* given value does not exist at all.
*
* #param List<E> list
* #param E value
* #return List<Integer> a list of indexes in the list
*/
public <E> List<Integer> collectFrequency(List<E> list, E value) {
ArrayList<Integer> freqIndex = new ArrayList<Integer>();
E item;
for (int i=0, len=list.size(); i<len; i++) {
item = list.get(i);
if ((item == value) || (null != item && item.equals(value))) {
freqIndex.add(i);
}
}
return freqIndex;
}
if (!collectFrequency(portList, someIntValue).size() > 1) {
// Duplicate value
}
Or using the already availble method:
if (Collections.frequency(portList, someIntValue) > 1) {
// Duplicate value
}
Set portSet = new HashSet<Integer>();
portSet.addAll(portList);
boolean listContainsDuplicates = portSet.size() != portList.size();
I used the following solution to find out whether an ArrayList contains a number more than once. This solution comes very close to the one listed by user3690146, but it does not use a helper variable at all. After running it, you get "The number is listed more than once" as a return message.
public class Application {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(4);
list.add(8);
list.add(1);
list.add(8);
int number = 8;
if (NumberMoreThenOnceInArray(list, number)) {
System.out.println("The number is listed more than once");
} else {
System.out.println("The number is not listed more than once");
}
}
public static boolean NumberMoreThenOnceInArray(ArrayList<Integer> list, int whichNumber) {
int numberCounter = 0;
for (int number : list) {
if (number == whichNumber) {
numberCounter++;
}
}
if (numberCounter > 1) {
return true;
}
return false;
}
}
Here is my solution (in Kotlin):
// getItemsMoreThan(list, 2) -> [4.45, 333.45, 1.1, 4.45, 333.45, 2.05, 4.45, 333.45, 2.05, 4.45] -> {4.45=4, 333.45=3}
// getItemsMoreThan(list, 1)-> [4.45, 333.45, 1.1, 4.45, 333.45, 2.05, 4.45, 333.45, 2.05, 4.45] -> {4.45=4, 333.45=3, 2.05=2}
fun getItemsMoreThan(list: List<Any>, moreThan: Int): Map<Any, Int> {
val mapNumbersByElement: Map<Any, Int> = getHowOftenItemsInList(list)
val findItem = mapNumbersByElement.filter { it.value > moreThan }
return findItem
}
// Return(map) how often an items is list.
// E.g.: [16.44, 200.00, 200.00, 33.33, 200.00, 0.00] -> {16.44=1, 200.00=3, 33.33=1, 0.00=1}
fun getHowOftenItemsInList(list: List<Any>): Map<Any, Int> {
val mapNumbersByItem = list.groupingBy { it }.eachCount()
return mapNumbersByItem
}
By looking at the question, we need to find out whether a value exists twice in an ArrayList. So I believe that we can reduce the overhead of "going through the entire list just to check whether the value only exists twice" by doing the simple check below.
public boolean moreThanOneMatch(int number, ArrayList<Integer> list) {
int count = 0;
for (int num : list) {
if (num == number) {
count ++ ;
if (count == 2) {
return true;
}
}
}
return false;
}