• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Messing with double arrays

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,
I had recently been trying to get into using double arrays. When I had these two strings in different arrays it ran fine, but when I try to integrate them into one giant array Eclipse says that I have an out of bound error. Thank you all for your time!

 
author & internet detective
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cardNumbers is an array of 2 arrays. The first contains 13 elements and the second contains 4 elements. So to get 6 of spades, you'd write:



Do you see how this affects your code?
 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the structure if cardNumbers? You have the first element (0) which is an array of 13 element. Then you have a second element (1) with an array of four elements. So how would you access it?
 
Marshal
Posts: 76412
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:What is the structure if cardNumbers? . . .

The way you have it, the structure looks wrong. There is a better way to have a predetermined number of options, which you can find examples of in the Java® Language Specification.
 
Thomas McAdams
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah I am still new to arrays. From the looks of it by the reference I better start over. Thank you all for the input though! I am starting to understand it now.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thomas McAdams wrote:Yeah I am still new to arrays. From the looks of it by the reference I better start over. Thank you all for the input though! I am starting to understand it now.


From the looks of it, you were thinking along the right lines; it's just your final result was a bit off. "2D" arrays (ie, <whatever>[][]) are usually used to contain "grids" of information, such as:
  private Square[][] chessBoard = new Square[8][8];
or:
  private int[][] matrix = new int[4][6]; // a matrix of 4 rows by 6 columns

However, "2D" is a bit of a misnomer. In Java, ALL arrays are one-dimensional; but each element of an array can itself be an array.

Thus, a String[][] is NOT a grid of Strings (as it is in some languages), it is an array of String[]s - each of which is an array of Strings.

And this isn't just "mechanical fluff"; it actually makes 'multi-dimensional' arrays much more flexible.
For example: suppose I wanted to read in the first hundred lines of a text file into my program and store it as a table of characters. I don't know what each line looks like, but I do know that the longest they can be is 100 characters.

If I was forced to create a two-dimensional array, it would have to look something like:
  private char[][] textCharacters = new char[100][100]; (100 lines of 100 characters each)
ie, 10,000 characters. Always. But in Java I can create a:
  private char[][] textCharacters = new char[100][]; (100 lines of unknown length)
and then read my file into it with:and now textCharacters will only take up as much space as the lines I read in.
Furthermore, if I read in a line that's longer than 100 characters, it will still work.

From the look of it, you probably already knew some of this, but I thought a concrete example might help.

Winston
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic