my dog learned polymorphism*
The moose likes Beginning Java and the fly likes 2 Dimmensional Array and asList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "2 Dimmensional Array and asList" Watch "2 Dimmensional Array and asList" New topic
Author

2 Dimmensional Array and asList

RC Springer
Greenhorn

Joined: Sep 17, 2007
Posts: 4
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.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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!


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
RC Springer
Greenhorn

Joined: Sep 17, 2007
Posts: 4
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Sep 17, 2007
Posts: 4
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

Joined: Sep 17, 2007
Posts: 4
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

Joined: Aug 31, 2004
Posts: 11343

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").
 
 
subject: 2 Dimmensional Array and asList
 
Similar Threads
How Do I Create An Array That "Backs" a List?
Question of Arrays.asList()
K&B doubt pg 559 toArray(),asList()
Another from collections
Coders at work - how was it made