0

1) I am unable to read the elements from the list Column Wise because the size of the Column is variable.

2)I have 10 Database and have stored each of these 10 database information in a list.Each list in the database can have variable length.

having variable data.

I am able to insert the elements in the list. But I am not able to read the elements from the list Column Wise but the rows are fixed.

For Example:Let's say the elements can be any.

1 2 3

1 2 3 4 5

1 2

1 2 4

I need the output as 1,1,1,1,2,2,2,2,3,3,4,4,5

Can anyone guide me How can I get?

MyUnderstanding

I tried the following approach and Coded like this

I MyCode:

    int ar[][] = new int[10][]; //Created a 2Dimensional Array
    //The Size of the row is fixed but not for Columns(as it is variable)

    ArrayList<Object> al = new ArrayList<Object>(); 

    ArrayList<<Object> al1 = new ArrayList<Object>();

    ArrayList<Object> al4 = new ArrayList<Object>();

    for (int j = 0; j < ar.length; j++) //This loop is for generating each database

       {
        System.out.println("Enter the Size of the list" + j);
        String s1 = sc.next();
        int i = Integer.parseInt(s1);
        for (int k = 0; k < i; k++)  //This loop is for generating the elements size inside each database
{
            System.out.println("Enter the Elements of the list" + j);
            String s2 = sc.next();
            al1.add(s2); //I store these elements in a list
            ar[i] = new int[al1.size()];
            al.add(al1);    //Finally store a list in a SeperateList

        }

    }



    System.out.println("Reading each elements of the ArrayList");

MAIN PROBLEM

    ListIterator ltr = al.listIterator(); // Used ListIterator to extract
                                            // each
                                            // List from the 10 list.

    for (int i = 0; i < 10; i++)

    {
        for (int l = 0; l < ar[i].length; l++) 
        {
            while (ltr.hasNext()) // A list AL having 10 Databases.
            {
                ArrayList a0to9 = (ArrayList) ltr.next();// I have taken 1 listfrom the 10 Databases.
                ListIterator ltr1 = a0to9.listIterator(); // Used ListIterator to extract each Element from the List.

                while (ltr1.hasNext()) // If the first List contains the elements (the elements are stored in the form of list)
                {
                    ArrayList elements = (ArrayList) ltr1.next(); // If the list has elements we can extract that Object
                    al4.add(elements); //And store the elements in another list
                    break;
                }

            }
        }
    }   

But I am getting the folloing Output

Output:

Exception in thread "main" java.lang.NullPointerException
at LockWaitTimeoutTest.main(LockTest.java:48)

Can anyone guide me how can I solve this problem.

Thankx in Advance :)

2 Answers2

0

A couple things to start with:

for (int j = 0; j < ar.length; j++)

{
    System.out.println("Enter the Size of the list" + j);
    String s1 = sc.next();
    int i = Integer.parseInt(s1);
    for (int k = 0; k < i; k++) {
        System.out.println("Enter the Elements of the list" + j);
        String s2 = sc.next();
        al1.add(s2);
        ar[i] = new int[al1.size()];
        al.add(al1);

    }

}
  • You never assign al1 within the for cycles, so it will in the end contain every answer to Enter the Elements of the list prompt
  • You never assign al1 within the for cycles, so every item in al list will point to the same list
  • You don't use index j when writing to array, so assigment to ar will always overwrite values from previous outer loops iterations
  • Since you always append one new element to al1 in each inner iteration, al1.size() will be growing by 1 in every inner iteration
Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
0

First, you should not work with raw types. You should exactly specify which types you expect a list to hold. Instead you can insert all elements (Object) and probably won't notice if you insert elements of different types than you have expected.

That is also where your problems lies. The true structure of your al is ArrayList<ArrayList<String>>. Since you add lists to al with al.add(al1);. A al1 is a ArrayList<String> since you add Strings here al1.add(s2);.

So far so good, so where is the problem? When processing, you expect al1 to hold an ArrayList instead of a String.

al.listIterator() --> (ArrayList) ltr.next() // ltr iterates over al
a0to9.listIterator() --> (ArrayList) ltr1.next(); // ltr1 iterates over al1

Next time you should use generics and not raw types, so that you immediately get a CompileError and see exactly where the problems are :)

Another thing, it may be easier for you to use the extended-for-loop instead of using a ListIterator. How does it work? Here's an example:

List<Element> list = ...;
...
for (Element element : list) {
    System.out.println(element);
}

If you need the index, you can go for this:

ArrayList<Element> list = ...;
...
for (int i = 0; i < list.size(); i++) {
    System.out.println("Index: " + i + ", Element: " + list.get(i));
}

But be aware that this is pretty slow if you have a List with slow, direct access. ArrayList has fast direct access, because it knows which elements are at a given index. For a LinkedList you should go with an Iterator as it does not have a direct access.

Summarized: Do not use raw types like ArrayList myList, but instead use generics like ArrayList<String> myList. Then you won't have problems debugging such stuff next time.

ragingasiancoder
  • 616
  • 6
  • 17
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • I will do the following changes but my problem lies in accessing the elements column wise in which the rows are fixed but the columns are not. – user6389648 Jul 14 '16 at 16:17