Solved

List of type of custom class into array list of integers

Posted on 2011-03-21
4
284 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
[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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 77
login form jsp example 2 73
Selenium docs api java index 3 98
Bot application - advice 3 81
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

752 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