Solved

How To - multi dimensional ArrayList of 2 ArrayLists ????

Posted on 2008-10-29
10
3,883 Views
Last Modified: 2012-06-22
Experts:

What's the (best) way/syntax to create an Array/ArrayList/List that consists of 2 ArrayLists?  
Example:

List<someObject> oldObject = new ArrayList<someObject>();
List<someObject> newObject = new ArrayList<someObject>();
// do stuff
// I want to return a Array/ArrayList/List containing
// List<someObject> oldObject, List<someObject> newObject
// something like this:  someObject[][] = new someObject[oldObject.size][newObject.size];
Anybody know how to do that?
0
Comment
Question by:pdlarue
  • 6
  • 4
10 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 22835134
     ArrayList[] a2 = new ArrayList[2];
      a2[0] = new ArrayList<String>();
      a2[1] = new ArrayList<String>();

JAVA refuses to create a generic 2 dimensional List-family,

List<String>[] = new ArrayList<String>()[];  // Is not possible.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 22835139
I meant:

List<String>[] = new ArrayList<String>()[2];  // Is not possible.

;JOOP!
0
 

Author Comment

by:pdlarue
ID: 22836837
OK.  Is it possible to create a multi-dimensional array that contains 2 ArrayLists?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:pdlarue
ID: 22836985
Is it possible to use multidimensional HashMap?  Something like this:

myHashMap.put (key, object1, object2);
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 22838504
Think: what would be the result of

        myHashMap.get(someKey) ??

It IS possible on the contrary to create a HashMap of HashMap's:

       HashMap<String, HashMap<String, MyObject>> h2 = new HashMap<String, HashMap>();

now you can:

       h2.put(thisKey, new HashMap<String, MyObject>());

;JOOP!
0
 

Author Comment

by:pdlarue
ID: 22843315
OK.  Your answers are great.  I know if anyone can point me in the right direction you can.  I may not be phrasing the question quite right.  This is the result I want.  Do you know of any way to do this?

newCustomer[0], oldCustomer[0]
newCustomer[1], oldCustomer[1]
newCustomer[2], oldCustomer[2]
newCustomer[3], oldCustomer[3]
newCustomer[4], oldCustomer[4]

   OR   (if HashMap is necessary)

"0",newCustomer[0], oldCustomer[0]
"1",newCustomer[1], oldCustomer[1]
"2",newCustomer[2], oldCustomer[2]
"3",newCustomer[3], oldCustomer[3]
"4",newCustomer[4], oldCustomer[4]


I am trying to bundlie this up in an Array or HashMap or something from the server-side code and send to the client-side code.  The gui developer is using Flex, which has the ability to get multiple objects in the same iteration (just like the first group of objects listed above).  Since this is a comparison between the old object and the new object (to include properties) the ListArrays will be the same size.

If you can tell me how to do this I'll bump up the points to 500.  Thank you for your help.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 22843928
A HashMap stores relations from 1 key to only 1 object.

You can of course put the next 'hidden' class at the bottom of your source:

class CustomerPair
{
     Customer oldC;
     Customer newC;

     public CustomerPair(Customer o, Customer n)
     {
          oldC = o;
          newC = n;
     }
}


now you can use code like this (ONLY IN THIS CLASS ABOVE THE HIDDEN CLASS!):

HashMap<String, CustomerPair> links = new HashMap<String, CustomerPair>();

// and:

links.put("Jensen", new CustomerPair(new Customer(p), new Customer(q)));

// Not very beautiful but it's a workable solution.
// And, please, don't praise me too much; I'm just an old programmer ..................

;JOOP!
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 500 total points
ID: 22843947
And, because one day you want the data back:

   CustomerPair t = links.get("Jensen");
   if(t != null)
   {
         Customer oldJensen = t.oldC;
.....

;JOOP!
0
 

Author Comment

by:pdlarue
ID: 22846500
Thank you.  I'll give it a try.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 22847937
Thanks, you gave me a promotion.

;JOOP!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
map related example 6 55
Groovy problem when using SOAPUI : DispatchException occurred 7 58
ejb example issues 3 26
jsp insert to database example 2 46
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues undeā€¦
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationā€¦
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question