Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Simplest mutable integer.

Posted on 2006-10-21
23
Medium Priority
?
2,213 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
0
Comment
Question by:PaulCaswell
  • 7
  • 6
  • 5
  • +2
23 Comments
 
LVL 1

Expert Comment

by:dbrownell83
ID: 17782117
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
 
LVL 1

Expert Comment

by:dbrownell83
ID: 17782120
or actually, that's not even necessary.  it should work your way
0
 
LVL 16

Author Comment

by:PaulCaswell
ID: 17782173
It doesnt seem to. Perhaps I have a bug. I will look deeper.

Paul
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Expert Comment

by:dbrownell83
ID: 17782204
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
 
LVL 1

Expert Comment

by:dbrownell83
ID: 17782213
i think it's a trick though.  it copies the reference by value, if you know what i mean...
0
 
LVL 1

Expert Comment

by:dbrownell83
ID: 17782224
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
 
LVL 28

Expert Comment

by:rrz
ID: 17782425
>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
 
LVL 9

Expert Comment

by:shinobun
ID: 17782753
dbrownell83's first post would be the easiest way:

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

Expert Comment

by:shinobun
ID: 17782759
Since Integer is not a mutable object, the value will never change.
0
 
LVL 9

Expert Comment

by:shinobun
ID: 17782765
>> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17782862
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
 
LVL 9

Expert Comment

by:shinobun
ID: 17782877
Is the Vector storing Integers, and is that what you want to change?  Then CEHJ's solution will do the trick.
0
 
LVL 16

Author Comment

by:PaulCaswell
ID: 17783033
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17783043
My last posting doesn't really have anything to do with offsets, nor, AFAICS, does the question as originally stated
0
 
LVL 16

Author Comment

by:PaulCaswell
ID: 17783373
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 252 total points
ID: 17783401
Easiest way is

changeIndex(new int[] {oldIndex});


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

Expert Comment

by:dbrownell83
ID: 17783452
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
 
LVL 16

Author Comment

by:PaulCaswell
ID: 17783499
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17783545
>>Is that technique common?

It is a way to simulate passing a primitive by reference, but i wouldn't say it's common
0
 
LVL 9

Assisted Solution

by:shinobun
shinobun earned 248 total points
ID: 17783557
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
 
LVL 9

Expert Comment

by:shinobun
ID: 17783578
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
 
LVL 1

Expert Comment

by:dbrownell83
ID: 17783667
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17783713
:-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question