How do you create a Two-Dimensional ArrayList?

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

Was this solution helpful to you?

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:

Just Added Q & A:

Find solution

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.