Create a program that stores the square of the numbers 0, 1, 2 & 9 in an ArrayList of 10 elements.?
-
For example, the third element which has an index of 2 should store 4.
-
Answer:
ArrayList<Integer> list = new ArrayList<Integer>(); list.add(0); list.add(1*=1); //A square is just a number times itself [ i*=i ] list.add(2*=2); list.add(9*=9); list.add(0); list.add(0); list.add(0); list.add(0); list.add(0); list.add(0); //This is for just 0, 1, 2, and 9. If you meant zero to nine then you do the same process for each Integer or variable. Which can be put into a loop: ArrayList<Integer> list = new ArrayList<Integer>(); Integer i = new Integer(0); for(i=0;i<10;i++) { list.add(i*=i); }
Murtaza at Yahoo! Answers Visit the source
Other answers
This is what it looks like using JavaScript: <html> <head> <script type="text/JavaScript"> var numbers = new Array(0, 1, 2, 9); var arrayList = new Array(10); for ( i = 0; i < numbers.length ;++i) { arrayList[i] = numbers[i] * numbers[i]; } for (i = 0; i< arrayList.length; ++i) { document.write("arrayList [" + i + "] = " + arrayList[i] + "<br>"); } </script> </head> </html> The output looks like this: arrayList [0] = 0 arrayList [1] = 1 arrayList [2] = 4 arrayList [3] = 81 arrayList [4] = undefined arrayList [5] = undefined arrayList [6] = undefined arrayList [7] = undefined arrayList [8] = undefined arrayList [9] = undefined
ray_diator
You won't get anyone doing your homework for you.
Arthur
Related Q & A:
- How to create a simple counter program?Best solution by Stack Overflow
- How many tickets can I get selecting 6 numbers from 1 to 40 for each play?Best solution by Yahoo! Answers
- Is there any way to connect USB in computers to Playstation 1/2/3's controller?Best solution by Yahoo! Answers
- How do you change layout 1.0 to 2.0?Best solution by freecodesource.com
- How do you multiple 1 2/3 by 3 1/8?Best solution by themathpage.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.