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
Solved

List of type of custom class into array list of integers

Posted on 2011-03-21
4
273 Views
Last Modified: 2012-06-21
I have an array list that containts variables of type of a custom class.
List<List<Point>> points = new ArrayList<List<Point>>();
I would like to convert it into an array list of integers. How to do it?
0
Comment
Question by:chudyksg
  • 3
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35180016
You mean List<List<Integer>>  ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35180041
... or do you mean

List<Integer> ?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 35180107
(Assuming the latter)
public List<Integer> pointsToIntegerList(List<List<Point>> points) {
        List<Integer> ints = new ArrayList<Integer>();

        for (List<Point> lp : points) {
            for (Point p : lp) {
                ints.add(p.x);
                ints.add(p.y);
            }
        }

        return ints;
    }

Open in new window

0
 
LVL 1

Expert Comment

by:HyMaX_2003
ID: 35180146
The Point class has two fields called "x" and "y", that represents your point's coordinates. My suggestion is to iterate through all the points and, as the process goes by, create a new array with the "x" and "y" values. So, your final array will be something like { x1, y1, x2, y2, x3, y3 ... }. A sample code to do it:

List<Point> points = new ArrayList<Point>();
List<Integer> points2 = new ArrayList<Integer>();

for (int i = 0; i < points.size(); i++)
{
    points2.add(points[i].x);
    points2.add(points[i].y);
}

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

791 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