c - What is a clean way to modify arguments inside a function in java? -
i interested in doing c code in java:
// sets n's ith bit right, starts 0 void setbit(int* n, int i){ *n = *n | (1 << i); } however, looks java can't pass addresses, clean approaches?
i thought of 2 approaches, wondering if there better ways it?
approach 1: using array
// sets n[0]'s ith bit right, starts 0 public void setbit(int[] n, int i){ n[0] = n[0] | (1 << i); } approach 2: using class
private class data{ int value; } // sets d.value's ith bit right, starts 0 public void setbit(data d, int i){ d.value = d.value | (1 << i); }
nope, no better way it...
unless you'd traditional java way, is
d = setbit(d, i); public int setbit(int d, int i) { return d | (1 << i); } this in java "modifying arguments function" inherently unclean. clean way find alternative modifying arguments.
(sometimes it'll inevitable, in case workarounds way go. said, if want write method modify(mydata), it's better add modify() method mydata's class.)
Comments
Post a Comment