Thursday, February 24, 2011

Java java

You should not alter the list while iterating through it.

Serializable and Externalizable both are used to persist the state of object into a file or byte stream.serializable is marker interface having no methods in it.while Externalizable have two methods readExternal(),writeExternal() to control the processing of seriliazation.externazable extends serializable interface.

Implementation of sockets is in Session Layer and JVM is in application Layer.

To get the details of the threads running use getAllStackTraces

The sole purpose of hashcode() is to come up with efficient way of finding Hash buckets. Once a bucket is determined the overridden equals method can be used to retrieve that element.

Without hashcode method, all the elements can be inside a single bucket and hence result in inefficient hashes.

A transient variable is a variable that cannot be serialized. For example if a variable is declared as transient in a Serializable class and the class is written to an ObjectStream, the val ue of the variable can't be written to the stream instead when the class is retrieved from the ObjectStream the value of the variable becomes null

http://technologiquepanorama.wordpress.com/2009/02/12/use-of-hashcode-and-equals/

Use Inheritance if its a "is-a" relationship. Ex : Apple is a Fruit.
Use Composition if its a "has-a" relationship. Ex : Customer has an Address.

Friday, February 11, 2011

My own shuffle array code

#include "iostream"
#include
#include
#include
#include
#include
using namespace std;

void shuffle(int A[],int size)
{
int tmp;
for(int i = 0; i {
srand ( time(NULL) );

int ran = rand()%(size-i);
tmp = A[size - i];
A[size - i] = A[ran];
A[ran] = tmp;
}
}

int main()
{
int A[10] = {1,2,3,4,5,6,7,8,9,10};
shuffle(A,9);

int j = 0;
while(j<9)
{
cout << A[j++] << " ";
}

getch();
return 0;
}

Really gud code for Most occuring number in the array.

map m;
int currentMax = -999;
int maxCount = 0;
for(int i=0;i<10;i++)
{
int updated = m[a[i]]++; //Increment the value of key for counting occurances
updated++; // due to post increment
if (maxCount < updated) {
maxCount = updated;
currentMax = i;
}
}

Thursday, February 10, 2011

My own finding all palendromes in a string

#include "iostream"
#include
using namespace std;

void prntstr(char *str,int j, int i)
{
while(j<=i) { cout << str[j]; j++; } cout << "\n"; } void isPalen(char* str, int pos) { int j =pos; int i = pos,count = 0; int k,l; for(i,j;(i<=11)&&(j>=0);i++,j--)
{
if(str[j] == str[i+1])
{
// cout << str[j] << " " << str[i+1] << "\n"; count++; k = j;l=i+1; } } // cout << count; if(count>0){
count = 0;
prntstr(str,k,l);
// cout << k << " " << l;
}
}

int main()
{
char* str = "heehasdiooi";
for(int i = 1; i< 11; i++)
{
isPalen(str,i);
// cout << str[i] << "\n";
}
getch();
}

My first dynamic programming code

courtesy Mr Kadane.


#include "iostream"
#include
using namespace std;
int maxsumdp(int A[],int n)
{
int sum = 0, maxsum = 0, start = 0, end = 0, fstart, fend;
for(int i = 0;i maxsum)
{
fstart = start;
maxsum = sum;
end=i;
}
if(sum < 0)
{
sum = 0;
start = end + 1;
}
}
cout << maxsum << " " << fstart << " " << end;
}

int main()
{
int A[] = {1,-3,1,3,-1,3,-5};
maxsumdp(A,7);
getch();

}