Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Java int swap function

Name: Anonymous 2016-03-17 1:17

People told me it can't be done. However I created the impossible, java int swap function.
import java.lang.reflect.*;

class IntSwap
{
static char n[];
static {
try{
Field field = Integer.class.getDeclaredFields()[3];
field.setAccessible(true);
n = (char[])field.get(1);
} catch(Exception e){}
}

public static void swap(int a, int b){
char t = n[a];
n[a] = n[b];
n[b] = t;
}

public static void main(String args[]) throws Exception {
int a = 1;
int b = 3;
int c = 7;
System.out.format("a = %d, b = %d, c = %d\n", a, b, c);
swap(a, b);
System.out.format("a = %d, b = %d, c = %d\n", a, b, c);
swap(b, c);
System.out.format("a = %d, b = %d, c = %d\n", a, b, c);
swap(a, c);
System.out.format("a = %d, b = %d, c = %d\n", a, b, c);
}
}

You C++/C# elitists can't mock my pass-by-value only language anymore

Name: Anonymous 2016-03-17 1:32

What's it do?

Name: Anonymous 2016-03-17 1:36

>>2

it swaps integers, duh

Name: Anonymous 2016-03-17 1:51

Name: Anonymous 2016-03-17 2:22

>>3
What does it swap them with?

Name: Anonymous 2016-03-18 12:45

Rewrite this to work with integers greater than 9.

Name: Anonymous 2016-03-18 13:44

>>3
IntAbstractCapitalistFactory with IntAbstractSocialistFactory

Name: Anonymous 2016-03-18 17:45

} catch(Exception e){}

Not ENTERPRISE QUALITY

Name: Anonymous 2016-07-26 17:14

class swappableInt {
int value;

swappableInt() {
value = 0;
}

void setVal(int n) {
value = n;
}

int getVal() {
return value;
}

static void swap(swappableInt x, swappableInt y) {
int temp = x.getVal();
x.setVal(y.getVal());
y.setVal(temp);
}

}

class intSwapTest {
public static void main(String[] args) {
swappableInt a = new swappableInt();
swappableInt b = new swappableInt();
a.setVal(5);
b.setVal(4);
swappableInt.swap(a,b);
System.out.println("Value of A:");
System.out.println(a.getVal());
System.out.println("Value of B:");
System.out.println(b.getVal());
}
}

Name: Anonymous 2016-07-27 6:31

why aren't you creating a new PositionIndependentInteger class which inherits Swappable interface? why aren't you creating integers with PositionIndependentIntegerFactory which extends AbstractSwappablesFactory? why aren't you then creating a SwappableSwappableFactory which extends AbstractSwappablesFactory and implements Swappable for easier creation of PositionIndependentPositionIndependentIntegerFactory?

Name: Anonymous 2016-07-27 6:52

(this interface intentionally not implemented)

Name: Anonymous 2016-07-27 8:15

brool ("cool" for us bloods)

Don't change these.
Name: Email:
Entire Thread Thread List