Is there an Iterator to loop over data structure in cycles?
Let's say there is an array:
int[] arr = {-1,5,7,-1,-1,-1}
I want to find index of first non -1 value from this array and starting to search from the random position (idx = random.nextInt(arr.length)). For example idx = 4;
So first check if arr[4] == -1, then if arr[5] == -1 and so on. If the end of the array reached then start from 0 position and continue until non -1 found. It is guaranteed that there will be at least one value not equal to -1 in the array.
This can be done so:
int idx = -1;
for (int i = random.nextInt(arr.length); ; i++) {
if (i == arr.length) {
/** start over */
i = 0;
}
if (-1 != arr[i]) {
idx = i;
break;
}
}
Or so:
int idx = -1;
int i = random.nextInt(arr.length);
do {
if (-1 != arr[i]) {
idx = i;
}
i == arr.length ? i=0 : i++;
} while (-1 == idx);
Is there an Iterator, that supports cycling (call next() , if the end of array reached then automatically start from 0)?
Limitations: 1) efficiency is not considered; 2) standard Java API is preferred.
in java API there is no such api which satisfy your problem but you can made it by your own.
what you can do is use List to create LinkedList. to solve your problem.
you can extend List to your class (CircularLinkedList extends List) & then override method hasNext() & getNext() thats all you need.
I don't think there are any iterators that let you know the index of the element as you call next(), so you'd have to keep track of the current index separately. You might be able to build up a "wrap-around" iterator using Guava's Iterators.concat (or some other third-party class) to concatenate an iterator over the trailing part of the array with an iterator over the leading part. However, I think the code is likely to be more complex than a simple for loop or two.
I believe there is no such circular Iterator that will automatically go to the beginning of the array once the end has been reached. I have created one below (not tested, and design is flawed), which requires an entirely new class of code, and is much longer than your short for/while loops.
public class MyCircularIterator<E> implements Iterator<E> {
private List<E> list;
private int pos;
public MyCircularIterator(List<E> list) {
this(list, 0);
}
public MyCircularIterator(List<E> list, int start) {
this.list = list;
pos = start;
}
public boolean hasNext() {
if(list.get(pos) != -1) return false;
return true;
}
public E next() {
if(hasNext()) {
E obj = list.get(pos);
pos = (pos + 1) % list.size();
return obj;
}
}
public void remove() {
list.remove(this.nextIndex);
}
}
Related
I am creating a class that has an array, and I want to implement methods add, remove, and replace.
But I don't want to use any built-in internals.
public class MySet {
public int set[];
private int size = 0;
public MySet(int size) {
this.set = new int[size];
}
public boolean add(int item) {
for (int i = 0; i < this.size(); i++) {
if (this.set[i] != 0) {
// add to array
}
}
this.size++;
return true;
}
public int size()
{
return this.size;
}
}
When you initialize an array with a fixed size in Java, each item is equal to 0. The part with if this.set[i] != 0 is where I am stuck to add an item.
Should I use a while loop with pointers? Such as:
public boolean add(int item) {
int index = 0;
while (index <= this.size()) {
if (this.set[index] != 0 || index <= ) {
// increase pointer
index++;
}
this.set[index] = item;
}
But if I have an array such as [7, 2, 0 , 1] in the list, it won't get the last item in the loop, which I need.
So, how is this usually done?
You should keep the current index for the size of populated elements which looks like you do. When you add the set[size]= item and increment size. Once size hits the preallocated size of your array you need to create a new array with increased size (can pick double the size for example) and copy old array to the new one.
I have an array of objects and i want to add elements in this array and simultaneously sort them in ascending order.Although I tried many compinations , I always take a java.lang.ArrayIndexOutOfBoundsException. Here is a part of my code :
public boolean insert(Person p)
{
for(int i=0;i<=size();i++)
{
if (c==0)
{
array[0] = p;
c++;
return true;
}
else
{
if (p.compareTo(array[i])==-1)
{
array[i]=p;
c++;
for(int j = size(); j>i; j--)
{
array[j]=array[j-1];
}
}
else if((p.compareTo(array[i])==1))
{
array[i+1]=p;
c++;
for (int j=(size()-1);j>= i+1; j--)
{
array[j+1]=array[j];
}
}
else
{
return false;
}
return true;
}
}
return false;
}
private int c;
private Person array[];
public SortedPersonList()
{
this.array = new Person[c];
}
public int size()
{
return c;
}
Remove the equal sign in for(int i=0;i<=size();i++). That is, change to
for(int i=0;i<size();i++)
Array indices go from 0 to size-1. So array[size()] is outside the array. Hence the error.
1) You are initializing your array to a size 0, and you never resize it. An array has a fixed size, so in order to populate it with items beyond its range, you should first create a larger array and copy the contents to it (This is how ArrayList works).
2) When you find the insertion point, you shouldn't override this position before keeping its value in a temp variable. (Also, I don't understand the third case in your code. Why do you insert if p is greater than array[i]? you should rethink your logic.)
3) you may want to use binary search to find the insertion point. It has a better performance than linear search.
Your code :
for(int i=0;i<=size();i++)
Write like
for(int i=0;i<size();i++) or for(int i=0;i<=size()-1;i++)
Currently working on a java program that does a bunch of functions, and one of those functions is to delete a range of alphabetized words. I am using animals as an example.
Here is the display list prior to executing the deleteRange function:
cat
chinchilla
horse
mouse
rat
I ask the program to delete chinchilla to mouse, but it doesn't include horse.
public boolean deleteRange(String start, String stop){
boolean result = false;
int begin = Find(start);
int end = Find(stop);
while(begin<end){
Delete(storage[begin]);
begin++;
result = true;
}
return result;
}
My delete function:
public boolean Delete(String value){
boolean result = false;
int location;
location = Find(value);
if (location >= 0) {
moveItemsUp(location);
numUsed--;
result = true;
}
return result;
}
My find function:
public int Find(String value) {
int result = -1;
int index = 0;
boolean found = false;
while ((index < numUsed) && (!found)) {
found = (value.equals(storage[index]));
if (!found)
index++;
}
if (found)
result = index;
return result;
}
My moveitemsup function:
private void moveItemsUp(int start){
int index;
for (index = start; index < numUsed-1; index++){
storage[index] = storage[index+1];
}
}
So, you are deleting array indices 1 -3 --
After you delete array index 1, your former index 3 now becomes 2 AND you are incrementing "begin" .. So now you delete index 2.. which in this case is mouse .. so horse gets skipped .
Instead, you want to keep the number of elements to delete ( end - begin + 1 )
int count = end - begin + 1;
while ( count > 0 ) {
Delete(storage[begin]);
}
something along those lines.
Why don't you try something like this if your list is in an ArrayList (no Java stuff on this box so this hasn't been tested) .
public boolean deleteRange( String start, String stop ) {
for(Iterator<String> iter = storage.iterator();iter.hasNext();) {
String element = iter.next();
if(element.compareTo(start) >= 0 && element.compareTo(stop) <= 0 ) {
iter.remove();
}
}
}
The problems I can see are:
problem 1
you increase begin index and "shrink (not really shrink)" the array at same time, after each deletion.
for example
[a,b,c,d,e,f], say you want to delete b-e index 1-4
first you deleted b (index 1) then you moveItemsUp after deletion. so array is:
[a,c,d,e,f,f], then you begin++ begin is 2, which pointing d, jumped over c
problem 2
if you fixed problem 1, there is another thing you should consider about, after deletion, the array would be [a,f,f,f,f,f] if it is what you wanted.
btw, why not consider to use LinkedList ? it should be much faster for deletion.
int begin = Find(start);
int end = Find(stop);
int count = end - begin;
for (int i=0; i <= count; i++){
Delete(storage[begin]);
result = true;
changed some code to this, made it work =)
I have a collection, and I want to implement the add() method, such that only positive integers can be added to the collection. The collection can hold 4 values, and I have used the code below to initialize every value as "-1".
public class Bag implements Collection {
private int[] elements;
public Bag() {
elements = new int[Runner.SIZE_OF_COLLECTION];
for (int i = 0; i < Runner.SIZE_OF_COLLECTION; i++) {
elements[i] = -1;
}
}
So far in the method add() below, I have this loop iterating through each element in the collection, and replacing each element that's less than 0 with the positive integer that I want to add ("toAdd").
The problem is, I only want to add the positive integer "toAdd" once, and without a break in the loop, the method replaces EVERY element "-1" in the collection with the positive integer. With the break in the loop, the method fails to add the positive integer at all. Any ideas on how I can get the method to add the positive integer to the collection only once?
public void add(int toAdd) {
for (int i = 0; i < Runner.SIZE_OF_COLLECTION; i++) {
if (elements[i] <= 0 && toAdd>0) {
elements[i] = toAdd;
}
break;
}
}
Thanks in advance!
Move the break into the if statement.
You could use an ArrayList instead of an int array. With an ArrayList, you could get the index of the first occurence of -1 and use the set method to add your new value at that index.
This replaces the first value from elements, which is equal or less than 0, with value from toAdd argument.
public void add(int toAdd) {
for (int i = 0; i < Runner.SIZE_OF_COLLECTION; i++) {
if (elements[i] <= 0 && toAdd>0) {
elements[i] = toAdd;
break;
}
}
}
The add method for the interface Collection takes an Object (or a generic type, which you have not specified). If you are trying to override / implement the collection interface method with your add method, then the method signature is incorrect, and it will never be called.
Your class needs to look like:
public class Bag implement Collection<Integer>
{
// ... other necessary methods
public boolean add(Integer i)
{
// your method...
}
}
And probably easier than your implementation would be to do:
public class Bag extends java.util.ArrayList<Integer>
{
#Override
public boolean add(Integer i)
{
if ((i != null) && (i > 0)) super.add(e);
}
}
You probably need to override the other add methods as well, although truthfully, it would be better to encapsulate the ArrayList instead of extending it.
Add a method void removeFirst(int newVal) to the IntegerList class that removes the first occurrence of a value from the list. If the value does not appear in the list, it should do nothing (but it's not an error). Removing an item should not change the size of the array, but note that the array values do need to remain contiguous, so when you remove a value you will have to shift everything after it down to fill up its space. Also remember to decrement the variable that keeps track of the number of elements.
Please help, I have tried all of the other solutions listed on this site regarding "removing an element from an array" and none have worked.
This method supports the same functionality as Collection.remove() which is how an ArrayList removes the first matching element.
public boolean remove(int n) {
for (int i = 0; i < size; i++) {
if (array[i] != n) continue;
size--;
System.arraycopy(array, i + 1, array, i, size - i);
return true;
}
return false;
}
Rather than write this code yourself, I suggest you look at Trove4J's TIntArrayList which is a wrapper for int[] You can also read the code for ArrayList to see how it is written.
You could do this:
int count; //No of elements in the array
for(i=0;i<count;i++)
{
if(Array[i]==element )
{
swap(Array,i,count);
if(count)
--count;
break;
}
}
int swap(int Array[],int i,int count)
{
int j;
for(j=i;j<=count-i;j++)
a[i]=a[i+1];
}
This is not the Full Implementation.You have to create a class and do this.
Using the method below
public static <TypeOfObject> TypeOfObject[] removeFirst(TypeOfObject[] array, TypeOfObject valueToRemove) {
TypeOfObject[] result = Arrays.copyOf(array, array.length - 1);
List<TypeOfObject> tempList = new ArrayList<>();
tempList.addAll(Arrays.asList(array));
tempList.remove(valueToRemove);
return tempList.toArray(result);
}
You can remove the first element of any array by calling the method as demonstrated in the below JUnit test.
#Test
public void removeFirstTest() {
// Given
Integer valToRemove = 5;
Integer[] input = {1,2,3,valToRemove,4,valToRemove,6,7,8,9};
Integer[] expected = {1,2,3,4,valToRemove,6,7,8,9};
// When
Integer[] actual = removeFirst(input, valToRemove);
// Then
Assert.assertArrayEquals(expected, actual);
}