Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

List of type of custom class into array list of integers

Posted on 2011-03-21
4
Medium Priority
?
297 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 2000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
This video teaches viewers about errors in exception handling.
Suggested Courses

722 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