Hi,

I have a doubt regarding a function myfunc(int i,int j) function. The return value of this function is a vector of type int[ ] .For Example if we pass myfunc(4, 2) it will return a vector {( 1,0) ,( 0,1) ,( 1,0) ,( 1,0) , ( 0,1 ), ( 1,0)}.

Can anyone suggest how can I access the individual values.

public static Vector<int[]> myfunc(int i,int j){

	             Vector<int[]> result=new Vector<int[]>();
		int signOfI;
		int signOfJ;
		double ii=new Double(Math.abs(i));
		double jj=new Double(Math.abs(j));
		if(i==0 && j==0)
		return result;
		if(i>0) signOfI=1; else signOfI=-1;
		if(j>0) signOfJ=1; else signOfJ=-1;
					if (j==0){
					for (int f=0;f<ii;f++){
					int[] point={signOfI,0};
					result.add(point);
					}
					return result;
				   }
				if (i==0){
				for (int f=0;f<jj;f++){
				int[] point={0,signOfJ};
				result.add(point);
				}
				return result;
				}
				Double a=1.0;
				Double b=1.0;
				double cross=((ii/jj)*(a-.5)+.5);
				while (b<=ii || a<=jj){
				if (b>cross){
				int[] point={0,signOfJ};
				result.add(point);
				a++;
				cross=((ii/jj)*(a-.5)+.5);
				}else if (b==cross){
				int[] point={signOfI,signOfJ};
				result.add(point);
				a++;
				b++;
				cross=((ii/jj)*(a-.5)+.5);
				}else if (b<cross){
				int[] point={signOfI,0};
				result.add(point);
				b++;
				}
				}
				return result;
						}

I am trying to call the above function using

Vector<int[]> resultq=new Vector<int[]>();
    
    resultq=myFunc(4,2);
     for(int i=0; i<resultq.size(); i++)
     {
      System.out.println(resultq.get(i));
     }

But using this I am not able to get correct values.

Dani AI

Generated

The vector returned by your function contains int[] entries (each array is a pair). Printing an array object directly prints the default Object.toString() form rather than its contents, which is why the console looked wrong — as explained. To actually read the numbers, retrieve the array and index into it.

A simple, robust way to print each pair:

for (int[] pair : resultq) {
    if (pair != null && pair.length >= 2) {
        System.out.println("x=" + pair[0] + ", y=" + pair[1]);
    } else {
        System.out.println("invalid pair");
    }
}

If you only need a single coordinate, index directly, e.g. retrieve the i-th element and read pair[0] or pair[1].

For clearer code and fewer mistakes, consider replacing int[] with a small typed class so each element self-documents and prints nicely:

static final class IntPair {
    final int x, y;
    IntPair(int x, int y) { this.x = x; this.y = y; }
    public String toString() { return "(" + x + "," + y + ")"; }
}

Then use a List<IntPair> instead of Vector<int[]> unless you specifically need synchronization.

Quick troubleshooting checklist for :

  • Verify every element added is non-null and length 2 (or use IntPair to avoid that class of errors).
  • Check loop bounds and off-by-one logic if the number of steps is unexpected.
  • Prefer primitive int arithmetic for counting/indices instead of unnecessary floating-point wrappers to avoid rounding surprises.
  • If thread safety is not required, prefer ArrayList over Vector for simpler semantics and performance.

Recommended Answers

All 2 Replies

When you get an element of that vector, you are getting an array of ints, which prints as an incomprehensible object reference. To print those in a readable way try the arrays.toString method to format the content of each array, eg

int[] arr = resultq.get(i);
System.out.println(Arrays.toString(arr));

System.out.println(Arrays.toString(resultq.get(i)));

Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.