将方法添加到Vector问题

时间:2017-12-02 01:02:29

标签: java arrays vector

我想通过使用我的addInt方法将随机生成的数字添加到由用户(xdata,ydata)确定的大小的向量中,它在我的调试中生成所需的数字,但它只返回0,0 ,.当我打印出来时

  System.out.println("Input the size of first array");
  int[] xdata = new int[sc.nextInt()];
  System.out.println("Input the size of second array");
  int[] ydata = new int[sc.nextInt()];

  /* this code got my desired results but I want to use a single method
  for(int i=0; i<xdata.length; i++)
     xdata[i]= (int)(Math.random() * 100);
  for(int i=0; i<ydata.length; i++)
     ydata[i]= (int)(Math.random() * 100);
     */

  IntVector x = new IntVector(xdata); //intVector is my class and contructor
  IntVector y = new IntVector(ydata); //I had to make for this

  x.addInt(xdata);
  y.addInt(ydata);

  System.out.println(x.toString()+"\nsorted: ");
  System.out.println(y.toString());



  ////*****my adding method********////
public class IntVector{
//vector constructor//
  public int[] addInt (int[] data){
  if (data.length == n) {  //n meaning my capacity
     IntVector tmp[] = new IntVector[n+10000]; 

     for(int i=0; i<data.length; i++)
        data[i]= (int)(Math.random() * 100);

    } return  data; 
      //all data[i] have randomly generated numbers
      //but it only prints out 0 for each value after.
   }                
 }

0 个答案:

没有答案