Solved

ArrayLists in ArrayLists/HashMaps?

Posted on 2016-09-23
4
75 Views
Last Modified: 2016-09-23
I am currently a student in AP Computer Science trying to write a program that makes Applets much easier to make. My teacher loves applets, and me and my classmates detest them. To do so, the best way that I can think of saving coordinates is through ArrayLists in Arraylists in HashMaps. However, I can't seem to get this to work.

The reason I need a ArrayList in an ArrayList is because when you make a polygon, the parameters include two arrays - one for the x coordinates, and one for the y coordinates. These two would originally be ArrayLists. Those ArrayLists need to be stored into an ArrayList that contains all of the paramters, which is stored in a HashMap that stores all of the polygons.

Lets take this example:
HashMap<String, ArrayList<Object>> polygons= new HashMap<String, ArrayList<Object>>();
polygons.put("Polygon 1", new ArrayList<Object>());
polygons.get("Polygon 1").add(new ArrayList<Integer>());

Open in new window


This gives me a runtime error if I were to try to read any of the ArrayLists, and I get a compile time error if I try adding something to the innermost ArrayList.

Am I going about this in the wrong way? Is there 1. a better way to do this, or 2. a way to make this way work?
0
Comment
Question by:Ben Esposito
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 28

Expert Comment

by:dpearson
ID: 41812860
You're close.

// This is fine:
polygons.put("Polygon 1", new ArrayList<Object>());

// This is not:
polygons.get("Polygon 1").add(new ArrayList<Integer>());

// Let's break this out:
List<Integer> list = polygons.get("Polygon 1") ; // This is returning a list already
list.add(new ArrayList<Integer>()) ; // This tries to add a list to a list - not what you want

// So maybe try something like this:
List<Integer> list = polygons.get("Polygon 1") ;
list.add(6) ; // This should work - assuming the list has already been added to the hashmap.  If it hasn't you'll get a null pointer exception here.

Does that help?

Doug
1
 

Author Comment

by:Ben Esposito
ID: 41813007

// This is not:
polygons.get("Polygon 1").add(new ArrayList<Integer>());

// Let's break this out:
List<Integer> list = polygons.get("Polygon 1") ; // This is returning a list already
list.add(new ArrayList<Integer>()) ; // This tries to add a list to a list - not what you want

But I do want to add a list to a list. Here is my organization of data:
HashMap<String, ArrayList<Object> polygons; <-- This contains all polygons
When a polygon is created, it needs three parameters: int[] xcoords, int[] ycoords, and int # of points
The first two parameters would originally be ArrayList<Integer>()s, and then the third element in the ArrayList<Object> (from polygons) would be the third parameter.
0
 
LVL 28

Accepted Solution

by:
dpearson earned 500 total points
ID: 41813062
If you want a list of lists then the map should be:

HashMap<String, ArrayList<ArrayList<Object>>>

so the values are lists of lists.

But that is a pretty ugly way to represent 3 values int[], int[], int

A much more natural representation would be:
HashMap<String, Polygon>

class Polygon {
   List<Integer> xcoords ;
   List<Integer> ycoords ;
   int nPoints ; // Actually redundant - xcoords.size() is this too
}

So you add a Polygon object to the map and then retrieve it, get the xcoords member and call "add" on it:

Polygon p = polygons.get("Polygon 1") ;
p.xcoords.add(6) ;
p.ycoords.add(12) ;
p.nPoints = p.nPoints+1 ;

Doug
2
 

Author Closing Comment

by:Ben Esposito
ID: 41813112
Thank you! This helps a lot. I have never made a class for making objects, so I'm glad I'm learning this. I started doubting how possible this would be, but now it seems much more doable.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

717 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