| |||
Setting up and using array of arrays
Arrays example from a Well House Consultants training course
More on Arrays [link]
Source code: Charlton.java Module: J705
import java.lang.Math; // For Random Numbers
/* --- Sample output ---------- wizard:jn10 graham$ java Charlton Contents of hand number 1 2 7 11 17 22 31 33 36 38 42 43 50 51 Contents of hand number 2 14 23 28 34 35 37 39 41 44 45 46 48 49 Contents of hand number 3 4 5 10 12 15 19 24 25 26 29 40 47 52 Contents of hand number 4 1 3 6 8 9 13 16 18 20 21 27 30 32 wizard:jn10 graham$ */ public class Charlton { public static void main(String[] args) { int [] pack = new int [52]; // Original Pack int [] shpack = new int [52]; // For pack after shuffling int k,j; for (k=0;k<52;k++) pack[k]=k+1; // Set up cards by number (1-52) // shuffle for (k=0;k<52;k++) { int drawn_card = (int) (Math.random()*(52-k)); // Choose at Random shpack[k] = pack[drawn_card]; // Add chosen card to shuffled pack[drawn_card]=pack[52-k-1]; // Move last card up to fill gap } // deal int [][] hands = new int [4][13]; // 4 hands of 13 cards (Bridge?) int nc = 0; for (j=0;j<13;j++) { // Outer loop - 13 times around for (k=0;k<4;k++) { // Inner loop - 4 times each hands[k][j]=shpack[nc++]; // Deal a card into the hands } } // Something else /* This sorts each hand ;-) */ for (k=0;k<4;k++) { // for each hand for (j=12;j>=0;j--) { // bubble sort for (int i=0;i<j;i++) { if (hands[k][i]>hands[k][i+1]) { // Do we need to swap 2 cards? int holder = hands[k][i]; // via temportary variable hands[k][i]=hands[k][i+1]; hands[k][i+1]=holder; } } } } // This could do with going in the "algorithms" section! // list out hands for (k=0;k<4;k++) { // Hands are outer loop this time! System.out.print("Contents of hand number "); System.out.println(k+1); for (j=0;j<13;j++) { // Card no. is the inner loop System.out.print(" " + hands[k][j]); // Print each card } System.out.println(""); // A new line between each hand } } } Learn about this subject
This module and example are covered on the following public courses:
* Learning to Program in Java * Java Bootcamp * Java Programming for the Web Also available on on site courses for larger groups Books covering this topic
Yes. We have over 700 books in our library. Books
covering Java and associated technologies are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Arrays" training module. You'll find a description of the topic and some
other closely related examples on the "Arrays" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Web site author
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. | |||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php4 • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |