Solved

Simplest mutable integer.

Posted on 2006-10-21
23
2,205 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
Comment Utility
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
Comment Utility
or actually, that's not even necessary.  it should work your way
0
 
LVL 16

Author Comment

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

Paul
0
 
LVL 1

Expert Comment

by:dbrownell83
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>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
Comment Utility
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
Comment Utility
Since Integer is not a mutable object, the value will never change.
0
 
LVL 9

Expert Comment

by:shinobun
Comment Utility
>> 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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 9

Expert Comment

by:shinobun
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Easiest way is

changeIndex(new int[] {oldIndex});


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

Expert Comment

by:dbrownell83
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
:-)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now