{"id":222,"date":"2012-03-08T13:35:08","date_gmt":"2012-03-08T02:35:08","guid":{"rendered":"https:\/\/learn-java-by-example.com\/?p=222"},"modified":"2012-03-08T13:36:03","modified_gmt":"2012-03-08T02:36:03","slug":"list-array","status":"publish","type":"post","link":"https:\/\/learn-java-by-example.com\/java\/list-array\/","title":{"rendered":"Using a List instead of an array"},"content":{"rendered":"

Lists and arrays can both be used to store ordered collections of data. Both have their strengths and weakness which we shall discuss in a later post. <\/p>\n

Previously we showed you how to generate Pascals Triangle<\/a> and in that Java example we used arrays to represent each row of the triangle.<\/p>\n

The following code shows what the code would look like if we replace the use of arrays with List’s.<\/p>\n

\r\npackage com.learnjava.math;\r\n\r\nimport java.util.ArrayList;\r\nimport java.util.List;\r\n\r\n\/**\r\n * Write a Java program that calculates the \r\n * first 10 lines of a Pascals Triangle\r\n * \r\n * @author https:\/\/learn-java-by-example.com\r\n *\r\n *\/\r\n\r\npublic class PascalTriangleUsingList {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\t\r\n\t\tint n = 10;\r\n\t\t\r\n\t\tList<Integer> row = new ArrayList<Integer>();\r\n\t\t\r\n\t\tfor (int i=0; i<n; i++) {\r\n\t\t\t\r\n\t\t\t\/\/ Calculate next row\r\n\t\t\t\r\n\t\t\trow = pascalRow(row);\r\n\r\n\t\t\t\/\/ Output row\r\n\t\t\t\/\/ First add some padding so triangle is centered\r\n\t\t\t\r\n\t\t\tfor (int j=0; j< n - i; j++) {\r\n\t\t\t\tSystem.out.print(" ");\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t\/\/ Now output the row values\r\n\t\t\t\r\n\t\t\tfor (int j=0; j< row.size(); j++) {\r\n\t\t\t\tSystem.out.print(row.get(j)+" ");\r\n\t\t\t}\r\n\t\t\tSystem.out.println();\r\n\t\t\t\r\n\t\t}\r\n\t}\r\n\r\n\tpublic static List<Integer> pascalRow(List<Integer> previous) {\r\n\t\t\r\n\t\t\/\/ Row is 1 element longer than previous row\r\n\t\t\r\n\t\tList<Integer> row = new ArrayList<Integer>(previous.size() + 1);\r\n\t\t\r\n\t\t\/\/ Initialise the list\r\n\t\t\/\/ Unlike arrays, a list is empty until we add values to it\r\n\t\t\r\n\t\tfor (int i=0; i<previous.size()+1; i++) {\r\n\t\t\trow.add(0);\r\n\t\t}\r\n\r\n\t\t\/\/ First and last numbers in row are always 1\r\n\t\t\r\n\t\trow.set(0, 1);\r\n\t\trow.set(row.size() - 1, 1);\r\n\t\t\r\n\t\t\/\/ The rest of the row can be \r\n\t\t\/\/ calculated based on previous row\r\n\t\t\r\n\t\tfor (int i = 1; i< row.size()-1; i++) {\r\n\t\t\trow.set(i, previous.get(i-1) + previous.get(i));\r\n\t\t}\r\n\t\t\r\n\t\treturn row;\r\n\t}\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

Lists and arrays can both be used to store ordered collections of data. Both have their strengths and weakness which we shall discuss in a later post. Previously we showed you how to generate Pascals Triangle and in that Java example we used arrays to represent each row of the triangle. The following code shows…<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[4],"tags":[43,63,62,6,36,37],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Yyl2-3A","jetpack-related-posts":[{"id":160,"url":"https:\/\/learn-java-by-example.com\/java\/pascals-triangle\/","url_meta":{"origin":222,"position":0},"title":"Pascals Triangle","date":"February 13, 2011","format":false,"excerpt":"Write a Java application that prints the first 10 lines of Pascals Triangle. Each row of a Pascals Triangle can be calculated from the previous row so the core of the solution is a method that calculates a row based on the previous row which is passed as input. Once\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":355,"url":"https:\/\/learn-java-by-example.com\/java\/printing-triangles-java\/","url_meta":{"origin":222,"position":1},"title":"Printing Triangles in Java","date":"November 23, 2015","format":false,"excerpt":"A common problem many new Java developers is to write a program that prints out a triangle. There a lots of variations on this problem but lets start with a simple case and then have a look at some possible variations. Probably the simplest case is a left aligned triangle\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":205,"url":"https:\/\/learn-java-by-example.com\/java\/counting-numbers\/","url_meta":{"origin":222,"position":2},"title":"Counting numbers","date":"September 12, 2011","format":false,"excerpt":"The assignment here is to read a text file containing a list of numbers. We need to report how many numbers were in the file, the sum of all the numbers, the average of all numbers, and the minimum and maximum number. First step is to read the file and\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":199,"url":"https:\/\/learn-java-by-example.com\/java\/pythagoras-theorem\/","url_meta":{"origin":222,"position":3},"title":"Pythagoras Theorem","date":"December 13, 2010","format":false,"excerpt":"One of our member students was asked to implement the Pythagoras Theorem using Java. This is a good opportunity to introduce the Math class which contains a collection of static methods for various mathematical functions. We need the square root method (sort) to calculate the hypotenuse, and can also use\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":214,"url":"https:\/\/learn-java-by-example.com\/java\/convert-inches-centimetres\/","url_meta":{"origin":222,"position":4},"title":"Convert Inches to Centimetres","date":"October 14, 2011","format":false,"excerpt":"The next example was kindly shared by one of our members. She had a Java Swing assignment to create a Swing GUI for converting Inches to Centimetres and had no idea where to start. We helped her break down the problem and come up with a solution. The resulting code\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":235,"url":"https:\/\/learn-java-by-example.com\/java\/check-java-string-integer\/","url_meta":{"origin":222,"position":5},"title":"How to check if a Java String is an integer?","date":"June 5, 2015","format":false,"excerpt":"The Integer class has a number of static methods for parsing strings. For the case where we want to check if if a string contains a valid integer we can use the method Integer.parseInt() and catch the exception that is thrown when the number cannot be parsed.","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/222"}],"collection":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/comments?post=222"}],"version-history":[{"count":4,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/222\/revisions"}],"predecessor-version":[{"id":330,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/222\/revisions\/330"}],"wp:attachment":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media?parent=222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/categories?post=222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/tags?post=222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}