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.

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)));

This article has been dead for over six months. Start a new discussion instead.