Solved

List of type of custom class into array list of integers

Posted on 2011-03-21
4
248 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
Comment Utility
You mean List<List<Integer>>  ?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
... or do you mean

List<Integer> ?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
Comment Utility
(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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now