0

Using java, I am supposed to create a program that stores the square of the numbers 0, 1, 2 & 9 in an ArrayList of 10 elements.

I have created part of the code that displays the numbers and its squares but the program goes straight down with all the numbers and does not look organized. Can someone help me write it out like like this instead:

number: 0 square: 0

number: 1 square: 1

number: 2 square: 4

Code

public static void main(String[] args) {
    int[] temp = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    for (int value : temp) {
        System.out.println(value);
    }

    for (int i = 0; i < temp.length; i++) {
        temp[i] = (int) Math.pow(temp[i], 2);
    }

    for (int value : temp) {
        System.out.println(value);
    }
}
2
  • Java output formatting for Strings Commented Apr 21, 2017 at 15:18
  • If this is supposed to use an ArrayList, where is its usage? Or did you not actually mean an ArrayList? Commented Apr 21, 2017 at 15:23

4 Answers 4

1

You need just one loop like this :

public static void main(String[] args) {
    int[] temp = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    for (int i = 0; i < temp.length; i++) {
        System.out.println(temp[i] + "\t" + (int)Math.pow(temp[i], 2));
    }
}

OutPut

0   0
1   1
2   4
3   9
4   16
5   25
6   36
7   49
8   64
9   81

If you want to store your results in an ArrayList you can use :

List<int[]> array = new ArrayList<>();//create a List which take an array of int

int arr[] = new int[2];//create a temporary array of 2 elements

for (int i = 0; i < temp.length; i++) {
    System.out.println("Number: " + temp[i] + " \tSquare: " + (int) Math.pow(temp[i], 2));
    arr[0] = temp[i];//add your number to your array pos 1
    arr[1] = (int) Math.pow(temp[i], 2);//add the power to the 2ed position
    array.add(arr);//add your array to your list

}
Sign up to request clarification or add additional context in comments.

5 Comments

I believe you can just do int[] temp = new int[10]; and then instead of outputting temp[i] just output i. I would also tend to use String.format("%2d\t%3f") in the output to align the numbers.
@KevinO At that point the array is not needed since it can all be done with a for loop.
The OP's question required placing the results in an array.
@KevinO what about my edit? another thing, the temp array can be {5,6,9,111,4777} so it can contain any numbers, so you can't use just i
@YCF_L, If the initial array could contain numbers other than 0...9, then you are correct using the loop iterator would not work. Depends upon the specification. Good addition on the use of the ArrayList, which I took as part of the OP's question, if not the original example.
0
public static void main(String[] args) {
   int[] temp = {0, 1,2,3,4,5,6,7,8,9};  

     // here you are printing all your numbers in the array, so the output will be: 
     // 0
     // 1
     // ...
     // 9
     for (int value : temp) {
         System.out.println(value);
     }  


     // after that, you calculate and store them in the same array; so your array now look like this:
     // [0,1,4,9,...,81]
     for (int i = 0; i < temp.length; i++) {
         temp[i] = (int) Math.pow(temp[i], 2);
     }  


     // here you are printing again your array
     // 0
     // 1
     // 4
     // ...
     // 81
     for (int value : temp) {
         System.out.println(value);
     } 
 }  

to get your desired outcome, you have to calculate the number before printing everything... one option will be to create another array

int[] square = new int[9];

calculate in the first for loop and save the result in the square array and print them, something like this:

for (int i = 0; i < temp.length; i++) {
    square[i] = (int) Math.pow(temp[i],2);
    System.out.println("number " + temp[i] + " square: " + square[i]);
}

Comments

0

Assuming the original question which referred to an ArrayList was correct (the OP's example only had an array), and assuming the results are supposed to be stored in the array (per the question), then the following will work:

public static void main(String[] args)
{
  // instantiate ArrayList
  List<Double> array = new ArrayList<>();

  // load the values
  for (int i = 0; i < 10; ++i) {
    array.add(i, Math.pow(i, 2));
  }

  // output
  for (int i = 0; i < array.size(); ++i) {
    System.out.println(String.format("%2d\t%3.0f", i, array.get(i)));
  }
}

Output:

0     0
1     1
2     4
3     9
4    16
5    25
6    36
7    49
8    64
9    81

Comments

0

Try this :

public static void main(String[] args) {
  int[] temp = {0, 1,2,3,4,5,6,7,8,9};  

  for (int value : temp) {  
         System.out.println("number : "value);  
  }  

  for (int i = 0; i < temp.length; i++) {  
    temp[i] = (int) Math.pow(temp[i], 2);  
  }  

  for (int value : temp) {  
    System.out.println(" square : " + value + "\n");
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.