Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Strings and Arrays  RSS feed

 
Timothy Han
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm learning Java through this video on Udemy.com. Now this instructor when he was teaching me about arrays typed this code:



Now I'm not sure what he is doing here, but I thought you have to declare each element, like [2] or [3] for example, with some sort of string, then concatenate and output the strings. But if you look at the code here he did not declare what texts[0], texts[1], and texts[2] with any string or "words". How can it still output "Hello there"? Shouldn't the output be "null"?

Is texts[0][1] = "Hello there;" within or part of the arrays of String[][] texts = new String[2][3];? Is that the reason why this code works and outputs "Hello there"?
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The statement String[][] texts = new String[2][3]; declares (makes "space" for, if you will) a 2 x 3 grid of elements. Since all these elements are of type String, their default value is null. Since the indecise of this grid start with 0, first "address" texts[0][0].

The statement texts[0][1] = "Hello there"; sets the element at the address [0][1]. If you think of it as a grid, this would be the first row, second column. Therefore System.out.println(texts[0][1]); simply outputs the element at those indecise.

(The words in quote are not technically exactly correct, but they make it easier to visualise.)

 
Timothy Han
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Knute, So you mentioned that texts[0][1] = "Hello there"; sets the element at the address [0][1]. But within this "address" what is the individual elements in [0] and [1]? Does "Hello there" represents the entirety of both [0] and [1] together?
 
Carey Brown
Bartender
Posts: 3022
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a 2 x 3 grid. address [0][1] means the cell at row 0 and column 1. All other cells that weren't set to the String remain set to null.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that row and column are here the other way round from what you see with pixels.
 
Timothy Han
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry i still don't understand this. Can you elaborate further or give me a different example that is more concrete?

 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note the array has a few odd features:-
  • 1: A comma at the end of a line before } in line 3.
  • 2: One element is a 0‑length array. This does not appear in the second printout.
  • 3: Member arrays of different lengths.
  • The Uniocde escapes are ‑ and ’
     
    Knute Snortum
    Sheriff
    Posts: 4091
    112
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Think of a grid on a spreadsheet. It has columns that are labeled A, B, C, etc. and rows numbered 1, 2, 3, etc. If you want to address one cell, you find its columns (say B), and its row (say 1). So the second column on the left, top row is B1. Notice that this is one cell with a column and a row. If you want to identify the spreadsheet name, you would write name!B1.

    Now in this analogy texts is name! and [0][1] is B1. Note that there are not two cells, only one with the address [0][1].

    Now this analogy isn't perfect, because [0] doesn't have to be column and [1] doesn't have to be row; in fact, I think most people think of [0] as row and [1] as columns, but ultimately, this doesn't matter. texts is the name of the grid and [0][1] is the address to one cell.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!