Java 阵列输入保存所有位置

Java 阵列输入保存所有位置,java,arrays,position,Java,Arrays,Position,我有一个代码,我试图保存一个数组中的输入,但当我这样做时,它保存了所有的位置。 如果我的用户输入是Klas、Philip和Comp,我得到的输出为: 公司的Klas Philip,增加了编号100 公司Klas Philip,增加编号101 公司Klas Philip,增加编号102 公司的Klas Philip,增加编号103 但我确实希望第一个用户输入保存为来自Comp的Klas Philip,并添加数字100,第二个输入保存在数字101上,依此类推。 但是现在Comp的第一个输入Klas

我有一个代码,我试图保存一个数组中的输入,但当我这样做时,它保存了所有的位置。 如果我的用户输入是Klas、Philip和Comp,我得到的输出为:

公司的Klas Philip,增加了编号100

公司Klas Philip,增加编号101

公司Klas Philip,增加编号102

公司的Klas Philip,增加编号103

但我确实希望第一个用户输入保存为来自Comp的Klas Philip,并添加数字100,第二个输入保存在数字101上,依此类推。 但是现在Comp的第一个输入Klas Philip保存了所有的数字

这是密码

    Class1[] all = new Class1[120];
    int quantity = 0;

        System.out.println("text: ");
        String x = keyboard.nextLine();

        System.out.println("text2:  ");
        String y = keyboard.nextLine();

        System.out.println("text 3: ");
        String z = keyboard.nextLine();

        Class1 p = new Class1(x, y, z);
        all[quantity++] = p;

        for (int i = 100; i < all.length; i++)
             System.out.println(p.getFirstName()+(" ")+p.getSurname()+" from " +p.getTeamName()+" with number " +(x) +" added");

        if (quantity == all.length){
            Class1[] temp = new Class1[all.length * 2];
            for (int i = 0; i < all.length; i++)
                temp[i] = all[i];
            all = temp;

    }
}   
Class1[]all=新的Class1[120];
整数数量=0;
System.out.println(“文本:”);
字符串x=键盘.nextLine();
System.out.println(“text2:”);
字符串y=键盘.nextLine();
System.out.println(“文本3:”);
字符串z=键盘.nextLine();
类p=新类1(x,y,z);
全部[数量++]=p;
for(int i=100;i

}

我发现很难弄清楚你想要实现什么。你能修改一下这个问题使它更清晰吗?将Class1数组的长度硬编码为120,这就是为什么会得到重复打印输出的原因

改变

for (int i = 100; i < all.length; i++)
     System.out.println(p.getFirstName()+(" ")+p.getSurname()+" 
     from " +p.getTeamName()+" with number " +(x) +" added");
for (int i = 100; i < all.length; i++)
     System.out.println(p.getFirstName()+(" ")+p.getSurname()+" 
     from " +p.getTeamName()+" with number " +(i) +" added");
  Scanner keyboard = new Scanner(System.in);
  List<Class1> all = new ArrayList<Class1>();
  int quantity = 0;
  while(quantity <2) // whatever the max number of entries should be
  {

        System.out.println("text: ");
        String x = keyboard.nextLine();

        System.out.println("text2:  ");
        String y = keyboard.nextLine();

        System.out.println("text 3: ");
        String z = keyboard.nextLine();

        Class1 p = new Class1(x, y, z);
        all.add(p);
        quantity++;
    }
        for (int i = 0; i < all.size(); i++)
             System.out.println(all.get(i).getFirstName()+(" ")+
             all.get(i).getSurname()+" from " +all.get(i).getTeamName()+" with number 
             " +(i) +" added");
text: 
Jan
text2:  
Klaas
text 3: 
Huntelaar
text: 
Tom
text2:  
Baker
text 3: 
Gallifrey
Jan Klaas from Huntelaar with number 0 added
Tom Baker from Gallifrey with number 1 added