Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: convert arraylist to 2D array

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default convert arraylist to 2D array

    Salam ,

    I'm new here , and i beleive I'll find my answer here ,
    i hope you will help me.

    I have data from a table in database.
    this table contains ( id, name , age , class ) columns.
    till now has 4 rows as well.


    I created a java class called student :

     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package PG;
     
    /**
     *
     * @author VAIO
     */
    public class Student {
        String id;
        String name;
        String age;
        String clas;
     
        public Student(String id, String name, String age, String clas) {
            this.id = id;
            this.name = name;
            this.age = age;
            this.clas = clas;
        }
     
        public String getId() {
            return id;
        }
     
        public void setId(String id) {
            this.id = id;
        }
     
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
     
        public String getAge() {
            return age;
        }
     
        public void setAge(String age) {
            this.age = age;
        }
     
        public String getClas() {
            return clas;
        }
     
        public void setClas(String clas) {
            this.clas = clas;
        }
     
        @Override
        public String toString() {
            return "Student{" + "id=" + id + ", name=" + name + ", age=" + age + ", clas=" + clas + '}';
        }
     
     
     
     
     
    }
    ------------------------------------- !

    then i created Arraylist contains tables records ( but ) i inserted them student by student :
    INFO: [Student{id=1, name=Ali, age=14, clas=A}, Student{id=2, name=Bdr, age=15, clas=B}, Student{id=3, name=Omar, age=13, clas=A}, Student{id=4, name=Bdr, age=12, clas=C}]

    --------------------------

    okey ! now i want to convert this arraylist to 2D array ,
    because i want to compare first row with the rest , and then second one with the rest .. so on.

     
           Object[][] ab = new Object[a.size()][4];
                for (i = 0; i <a.size(); i++) {
                   // will read the row 
                    System.out.print("i value:" + i);
                    // will read col 
                    for (j = 0; j < 4; j++) {
                        System.out.print("i value:" + i + "," + "j value:" + j);
                        int y = 4 * i;
                    //    ab[i][j] = a.get(y + j);
                        ab[i][j] = [[B] i could not complete this part[/B] ] ! 
     
                    }
                }

    please will you help me ,
    i hope you understand my point
    thanks in advance.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: convert arraylist to 2D array

    Why take the data out of the class object? Is there a problem working with an arraylist?
    There are other util classes and methods that can be used with arraylists.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: convert arraylist to 2D array

    i have no problem with using arraylist ,
    but i think in this way comparing will be much easier especially I'll compare row by row.
    my question is the best way of converting arraylist to 2d arrays.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: convert arraylist to 2D array

    One way would be to use a loop. What was the problem you had?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: convert arraylist to 2D array

    Quote Originally Posted by Norm View Post
    One way would be to use a loop. What was the problem you had?
    yes i know ,

    look here

     
      Object[][] ab = new Object[a.size()][4];
                for (i = 0; i <a.size(); i++) {
                   // will read the row 
                    System.out.print("i value:" + i);
                    // will read col 
                    for (j = 0; j < 4; j++) {
                        System.out.print("i value:" + i + "," + "j value:" + j);
                        ab[i][j] = [[B] i could not complete this part[/B] ] ! 
     
                    }
                }

    i don't know how to fill the 2d arrays from arraylist ??
    and i guess i did something wrong !

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: convert arraylist to 2D array

    What data goes in the columns on a row? How can the program get that data from the objects?
    The way the code is written, the class needs to have a method that takes an int value (j) and returns some data to go in the column at position j in the array.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] How convert sting name as an arrayList name?
    By Purple01 in forum Java Theory & Questions
    Replies: 0
    Last Post: November 27th, 2012, 07:46 AM
  2. using ArrayList to hold double, convert from object
    By aueddonline in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 24th, 2011, 08:22 AM
  3. ArrayList<String> convert to lowercase
    By s_mehdi76 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2010, 09:05 PM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. convert arraylist to a hash map
    By nadman123 in forum Collections and Generics
    Replies: 1
    Last Post: July 29th, 2009, 04:24 AM

Tags for this Thread