Solved

List of type of custom class into array list of integers

Posted on 2011-03-21
4
266 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java.io.BufferedReader   readLine()  //returns the entire file ? 4 72
JavaFX TableView not displaying correctly 3 16
ejb on wildfly 5 19
mysql jsp example issue 32 32
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

813 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

18 Experts available now in Live!

Get 1:1 Help Now