• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2220
  • Last Modified:

Simplest mutable integer.

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
0
PaulCaswell
Asked:
PaulCaswell
  • 7
  • 6
  • 5
  • +2
2 Solutions
 
dbrownell83Commented:
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);
}

0
 
dbrownell83Commented:
or actually, that's not even necessary.  it should work your way
0
 
PaulCaswellAuthor Commented:
It doesnt seem to. Perhaps I have a bug. I will look deeper.

Paul
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
dbrownell83Commented:
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
0
 
dbrownell83Commented:
i think it's a trick though.  it copies the reference by value, if you know what i mean...
0
 
dbrownell83Commented:
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
0
 
rrzCommented:
>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());
  }
0
 
shinobunCommented:
dbrownell83's first post would be the easiest way:

int functionThatCanChangeI(Vector vector, Integer i) {
  // your stuff
  Integer newInteger = ...;
  // your stuff
  return newInteger;
}
0
 
shinobunCommented:
Since Integer is not a mutable object, the value will never change.
0
 
shinobunCommented:
>> 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.
0
 
CEHJCommented:
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)
0
 
shinobunCommented:
Is the Vector storing Integers, and is that what you want to change?  Then CEHJ's solution will do the trick.
0
 
PaulCaswellAuthor 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
0
 
CEHJCommented:
My last posting doesn't really have anything to do with offsets, nor, AFAICS, does the question as originally stated
0
 
PaulCaswellAuthor 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
0
 
CEHJCommented:
Easiest way is

changeIndex(new int[] {oldIndex});


public void changeIndex(int[] oldIndex) {
    oldIndex[0] += 10;
}
0
 
dbrownell83Commented:
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--;}

}
 
0
 
PaulCaswellAuthor 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
0
 
CEHJCommented:
>>Is that technique common?

It is a way to simulate passing a primitive by reference, but i wouldn't say it's common
0
 
shinobunCommented:
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);
}
0
 
shinobunCommented:
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;
}
0
 
dbrownell83Commented:
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--;}

}
 
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 7
  • 6
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now