-5

I have a loop that displays 10 values: 1.0, 1.1, 1.2, etc.:

int i = 0;
int x = 10;
for(i;i>x;i++)
{
   System.out.println(x);
}

but instead of displaying the values, I want to put them in an array. How do I do that?

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • Possible duplicate of [How to create a generic array in Java?](http://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java) – Timothy Truckle Jan 01 '17 at 19:01
  • 2
    Welcome to Stack Overflow! Please take the [tour](http://stackoverflow.com/tour), have a look around, and read through the [help center](http://stackoverflow.com/help), in particular [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) and [What topics can I ask about here?](http://stackoverflow.com/help/on-topic). – Timothy Truckle Jan 01 '17 at 19:02
  • 1
    Your loop does not do what you claim. – ruakh Jan 01 '17 at 19:08
  • Your loop does not execute once, since `i > x` is `false`. You meant `i < x`. – thatguy Jan 01 '17 at 19:10

2 Answers2

2

How about:

// You want x ints.
int x = 10;

// Make an array big enough to hold x ints.
int[] array = new int[x];

// Loop x times.
for(int i = 0; i < x; i++) {

   // Put the next number into the array.
   array[i] = i;

}
rossum
  • 15,344
  • 1
  • 24
  • 38
thatguy
  • 21,059
  • 6
  • 30
  • 40
0

first your way in writing for loop is need to be more clean

it should :

 for(int i=0; i > x; i++){
   System.out.println(x);
 }

second your boolean condition in for loop isn't true because x=10 is always bigger than i=0 so it won't print any thing.

third to put the values in array :

simply define array : int[] numbers = new int[size of array];

then put each value inside the index i of array :

numbers[i] = i;

finally for loop will be like:

for(int i=0; i < x; i++){
       numbers[i] = i;
}
Oghli
  • 2,200
  • 1
  • 15
  • 37