• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

2 Dimmensional Array and asList

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question from a book that I have been reading, How To Program in Java. The frustrating thing for me is that after reading the entire chapter multiple times I don't know what the answer is. I have done searching on the topic but haven't found anything that would help me with the question. So before I go take this book and put some gasoline on it and add throw it into the fire pit can someone help me?

Can a two-dimmensional array be passed to Arrays method asList?

How is this done, and any information you can provide to me on this subject would be appreciated.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rick Campbell:
...Can a two-dimmensional array be passed to Arrays method asList?

How is this done, and any information you can provide to me on this subject would be appreciated.


Welcome to JavaRanch!

In Java, multi-dimensional arrays are really just single-dimension arrays that contain other arrays. For example, if you have a 2-dimensional array like String[][] myStrings, then "myStrings" references a single-dimension array that holds String arrays.

I hope this helps enough to avoid pyrotechnics!
 
RC Springer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I run the program mentioned above I get the output of:

[[Ljava.lang.String;@1ad086a, [Ljava.lang.String;@10385c1]

Anyway I tried to play with it to access just one individual element but still didn't have much luck. This is what I did:

import java.util.*;

public class LinkTester {

public static void main(String[] args) {
// 2-dimensional array is an array of arrays...
String[][] myStrings = { { "abc", "def" }, { "ghi", "jkl" } };

// so the list holds single-dimension String arrays...
List<String[]> list = Arrays.asList(myStrings);
System.out.println("The answer is:" + list.get(1) );
}
}

It returned:
The answer is:[Ljava.lang.String;@12dacd1

Is that the memory location the value is being stored? I will continue to do testing but the fire pit looks better and better.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by RC:
When I run the program mentioned above I get the output of:

[[Ljava.lang.String;@1ad086a, [Ljava.lang.String;@10385c1]...


Right. This might look cryptic, but it's telling you that the two objects in this List are String arrays. (Arrays inherit their implementation of toString() from Object, which is the class, followed by '@' and the hex representation of the hashCode. The single bracket at the beginning of the class name tells you it's a single-dimension array.)

Because the list contains arrays, you can get to the elements in those arrays by treating them as arrays.

PS: I need you to take a look at the JavaRanch Naming Policy, and change your display name back to something that fits our policy. Thanks!
 
RC Springer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I updated my name to live by the rules.

Thanks for the information. I toyed around with the program to print out a specific element like:

import java.util.*;

public class ListTester
{

public static void main(String [] args)
{
// 2-dimensional array is an array of arrays...
String[][] myStrings = { {"abc", "def"}, {"ghi", "jkl"} };
System.out.println("myStrings is a 2-d array: " + myStrings);

// so the list holds single-dimension String arrays...
List<String[]> list = Arrays.asList(myStrings);
System.out.println("This list contains: " + list.get(1)[0] );

This prints out:
myStrings is a 2-d array: [[Ljava.lang.String;@e48e1b
This list contains: ghi

Trying to sum this up so I can show a level of understanding on what is happening. The list.get(1)[0] syntax is actually getting the entire second array, and I specify which column I want return by the [0]. Is this correct? I think I am mixing concepts up here but not sure.

First Array is: abc def
Second Array is: ghi jkl

I do know that I really don't want to hard code the index values because I could screw up and cause an error with a statement like:

list.get(0)[2]
 
RC Springer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I couldn't refuse to work on adding a little exception handling, pretty cool what you learn in a day.

import java.util.*;

public class ListTester
{

public static void main(String [] args)
{
// 2-dimensional array is an array of arrays...
String[][] myStrings = { {"abc", "def"}, {"ghi", "jkl"} };
System.out.println("myStrings is a 2-d array: " + myStrings);

// so the list holds single-dimension String arrays...
List<String[]> list = Arrays.asList(myStrings);
try
{
System.out.println("This list contains: " + list.get(0)[2] );
}
catch (ArrayIndexOutOfBoundsException exception)
{
System.out.println("You used an invalid index value you dope!");
}

Returns:
myStrings is a 2-d array: [[Ljava.lang.String;@1ad086a
You used an invalid index value you dope!
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by RC Springer:
...The list.get(1)[0] syntax is actually getting the entire second array, and I specify which column I want return by the [0]. Is this correct? I think I am mixing concepts up here but not sure...


list.get(1) references the second element in the list (the first element is at index 0). This element is a a String array, { "ghi", "jkl" }.

So list.get(1)[0] references the first element in that array ("ghi"), and list.get(1)[1] references the second element ("jkl").
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic