Solved

Simplest mutable integer.

Posted on 2006-10-21
23
2,211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 27

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 63 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 62 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

623 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