static int [] hashset_to_int_array(HashSet<Integer> hs){
int array[] = new array[hs.size()];
Iterator itr = hs.iterator();
int i=0;
while(itr.hasNext()){
array[i++] = Integer.parseInt(itr.next().toString());
}
return array;
}
Monday, April 11, 2011
Add hashset integers to int array in java
take mirror image of a int array in java
Okay so lets say you have a array and you just wanna take the mirror image of it. how do you do it ? well there are several ways of doing it. One way is to just make another array , copy the first one to the second one by looping through the first one in the reverse direction. like this.
Now you have to insert the above two arrays within a for loop to make the copy in reverse direction.
wow now the secondArray is the mirror image of the first one. This method as you can see does not modify the firstArray. What you can do is make firstArray refer to the secondArray. Like this
Now you may be wondering what happened to the firstArray memory segment. Well that will be garbage collected by the garbage collector because there is no reference to it in the code. Hey but this method is not recommended because it makes another copy of the original.
The one iam gonna show you below actually does the modification within the first array itself without requiring another array. but this method is a bit tricky. If i have time in the future i will show you how this code segment works using a diagram.
// this is the first array
int firstArray [] = {5 , 7, 98 , 44, 33 };
// this is how you make the second array
int secondArray[] = new int[firstArray.length];
Now you have to insert the above two arrays within a for loop to make the copy in reverse direction.
for(int i = firstArray.length - 1 ; i >= 0 ; i--){
secondArray[firstArray.length - 1- i]=firstArray[i];
}
wow now the secondArray is the mirror image of the first one. This method as you can see does not modify the firstArray. What you can do is make firstArray refer to the secondArray. Like this
firstArray = secondArray;
Now you may be wondering what happened to the firstArray memory segment. Well that will be garbage collected by the garbage collector because there is no reference to it in the code. Hey but this method is not recommended because it makes another copy of the original.
The one iam gonna show you below actually does the modification within the first array itself without requiring another array. but this method is a bit tricky. If i have time in the future i will show you how this code segment works using a diagram.
public static void mirror_int_array(int[] array) {
if (array.length % 2 == 0) {
int temp = 0;
for (int i = 0; i < array.length / 2 - 1; i++) {
temp = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = temp;
}
}
else {
int temp = 0;
for (int i = 0; i < (array.length + 1) / 2 - 1; i++) {
temp = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = temp;
}
}
}
swap 2 integers in int array when index is specified java
public static void swap_int_array(int []array , int index1 , int index2){
int temp=array[index1];
array[index1] = array[index2];
array[index2]= temp;
}
rotate a int array by specific amount java
public class rotate {
public static void rotate_int_array(int []array , int amount){
int array_size = array.length ;
if(amount % array_size ==0) return;
if(amount < 0){
amount = (-1) * amount;
int temp=0;
for(int i =0 ; i < amount ; i++){
temp=array[array.length-1];
for(int shift=array.length-1; shift >0 ; shift--){
array[shift] = array[shift-1];
}
array[0] = temp;
}
}
else{
if(amount > array_size) amount = amount % array_size;
int temp=0;
for(int i =0 ; i < amount ; i++){
temp=array[0];
for(int shift=0; shift < array.length -1 ; shift++){
array[shift] = array[shift+1];
}
array[array.length -1] = temp;
}
}
}
}
Saturday, April 9, 2011
remove numbers that are outside the range from int array java
The code below removes any negative numbers from the supplied array that are outside the range and returns a new array.
static int[] remove_outside_range(int array[] , int lower_bound , int upper_bound){
ArrayList<Integer> al = new ArrayList<Integer>();
for(int i=0; i < array.length; i++){
if(array[i] > lower_bound){
al.add(array[i]);
}
if(array[i] < upper_bound){
al.add(array[i]);
}
}
int new_array[] = new int[al.size()];
for(int i =0 ; i < al.size() ; i++){
new_array[i] = al.get(i);
}
return new_array;
}
Subscribe to:
Posts (Atom)