We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Simplest mutable integer.

PaulCaswell
PaulCaswell asked
on
Medium Priority
2,236 Views
Last Modified: 2013-12-14
Hi,

New to Java, old to programming. :-)

What's the tidy equivalent to passing an integer by reference.

I want to walk a vector doing something like this:

// Using int i would definitely not work.
for ( Integer i = 0; i < vector.size(); i++ ) {
  functionThatCanChangeI(vector, i);
}

How can this be done most neatly?

Paul
Comment
Watch Question

Java only passes by reference.  So, you want to do something like this:

for ( Integer i = 0; i < vector.size(); i++ ) {
  i = functionThatCanChangeI(vector, i);
}

or actually, that's not even necessary.  it should work your way

Author

Commented:
It doesnt seem to. Perhaps I have a bug. I will look deeper.

Paul
hmm i was wrong:

"Java does manipulate objects by reference, and all object variables are references. However, Java doesn't pass method arguments by reference; it passes them by value. "

Well,

i noticed you are incrementing an Integer.  does that even work?
Also, you should use an iterator, though i believe looping is ok for Vector.  It's not ok for List objects
i think it's a trick though.  it copies the reference by value, if you know what i mean...
and indeed, there is no iterator method for Vector.

for ( int i = 0; i < vector.size(); i++ ) {
  functionThatCanChangeI(vector, new Integer(i));
}

im not really sure what youre trying to do
rrzstudent
CERTIFIED EXPERT

Commented:
>and indeed, there is no iterator method for Vector.  
Not true. Vector inherits it from AbstractList.
Vector also has elements() to return an  Enumeration. If you wanted to print  them for example
  Enumeration e = v.elements();
  while(e.hasMoreElements()){
                             System.out.print(e.nextElement());
  }
  Iterator i = v.iterator();
  while(i.hasNext()){
                      System.out.print(i.next());
  }

Commented:
dbrownell83's first post would be the easiest way:

int functionThatCanChangeI(Vector vector, Integer i) {
  // your stuff
  Integer newInteger = ...;
  // your stuff
  return newInteger;
}

Commented:
Since Integer is not a mutable object, the value will never change.

Commented:
>> im not really sure what youre trying to do

I have the same question.  It looks as if you want to loop through a Vector, but you might want to jump back and forth in the process, in which case simply using an Iterator or Enumeration will not work.

Maybe there is a better way to do what you are trying to do.
CERTIFIED EXPERT
Top Expert 2016

Commented:
You don't need to use a mutable Integer as the Vector is mutable

public int functionThatCanChangeI(Vector v, int index) {
    int n = 0;
    if ((n = v.get(index)) > 10) {
        n = 0;
        v.set(index, n);
    }
    return n;
}

(Assumes Java >= 1.5)

Commented:
Is the Vector storing Integers, and is that what you want to change?  Then CEHJ's solution will do the trick.

Author

Commented:
Thanks people,

I understand the idea of returning a new offset. That would be limiting and annoying, especially as the inner function is recursive.

You are correct that I cant use an iterator as I need to look at the same record several times.

Here's what I have but I really dont like it. There should be a tidy way of doing this:

  public class Mint {
    public int i;
    Mint (int i) {this.i=i;}
    int inc() {i+=1;return i-1;}
    int dec() {i-=1;return i+1;}
    int add(int x) {i+=x;return i-x;}
    int sub(int x) {i-=x;return i+x;}
    int ipp() {i+=1;return i-1;}
    int imm() {i-=1;return i+1;}
    int ppi() {i+=1;return i;}
    int mmi() {i-=1;return i;}
  }

Paul
CERTIFIED EXPERT
Top Expert 2016

Commented:
My last posting doesn't really have anything to do with offsets, nor, AFAICS, does the question as originally stated

Author

Commented:
I must have been unclear.

It is the offset into the array that I wish the function to change. The C equivalent of what I want to do is something like:

void FunctionThatCanChangeI(int * i) {
 ...
 *i += 1;
 ...
}

int i = 0;
for ( i = 0; i < limit; ) {
 FunctionThatCanChangeI(&i);
}

Paul
CERTIFIED EXPERT
Top Expert 2016
Commented:
Easiest way is

changeIndex(new int[] {oldIndex});


public void changeIndex(int[] oldIndex) {
    oldIndex[0] += 10;
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
public class theClass {

   private static int mIndex = 0;
   public static int getNdx() {return mIndex;}
   public static void setNdx(int n) {mIndex = n;}
   
   public static void incNdx() {mIndex++;}
   public static void decNdx() {mIndex--;}

}
 

Author

Commented:
Ok so are you saying there is no mutable integer object I can use like an integer?

CEHJ,

Is that technique common? I've never noticed it used. I agree it is simple but it is far from intuitive.

dbrownell83,

Why is everything static? How does this style improve on my 'attempt'?

Paul
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>Is that technique common?

It is a way to simulate passing a primitive by reference, but i wouldn't say it's common
Commented:
To use CEHJ's method, you would do this in your caller method, as changing the array will not automatically change your index:

for ( Integer i = 0; i < vector.size(); i++ ) {
  int[] mutableInt = new int[] {i};
  functionThatCanChangeI(vector, mutableInt);
  i = mutableInt[0];
}

Or more simply,

for ( int[] i = new int[] {0}; i[0] < vector.size(); i[0]++ ) {
  functionThatCanChangeI(vector, i);
}

Commented:
What are you trying to accomplish in functionThatCanChangeI()?  I'll bet there's a better way to do it.

From all the information you currently provided, I would create an Iterator that permits random access.  (For an "intuitive" solution.)

interface RandomAccessIterator extends Iterator {
    void moveForward(int size) throws NoSuchElementException;
    void moveBackward(int size) throws NoSuchElementException;
}
sorry, it shouldnt be static.  though you should change your member variable to private.

If you're trying to change the index of something youre iterating, then it's an iteration problem, like shinobun says.  otherwise, i'm sure it's something simple.

public class theClass {

   private Vector mVector = new Vector(10);
   private int mIndex = 0;

   public int getNdxContents() {return mVector[mIndex];}
   public void setNdxContents(int i) {mVector[mIndex] = i;}

   public int getNdx() {return mIndex;}
   public void setNdx(int n) {mIndex = n;}
   
   public void incNdx() {mIndex++;}
   public void decNdx() {mIndex--;}

}
 
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.