1,105,423 Community Members

Convert Arraylist into a 2d Array?

Noopit
Newbie Poster
1 post since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

I Basically wanted to take an input and store it into a 2D Array called mat with a 3x3 size

so i tried taking an input string, convert them into char and then sotring them in an array list and this is what i wrote so far.

System.out.println("Enter Message");
        Scanner Scanner1 = new Scanner(System.in);
        String plaintext = Scanner1.next();
        int counter = 0;

        while(plaintext.length()>0){          // will get chars till the end of input length

        char text = plaintext.charAt(counter);
        counter++;

        ArrayList mylist = new ArrayList();

        mylist.add(text);


        System.out.println(mylist);          // chars of string

        }

        // How can i convert this ^^ list into A 2D Array??
    }

The output of the Arraylist is something like this
[H]
[e]
[l]
[l]
[o]

and i want it to be something ike

[H][e][l]
[l][o][w]
[x][y][z]

i just need to convert my input string into the 2d form of array and ive been trying alot i cant get it right :(

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,751 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

In pseudo code it looks like:

for i = 0 to 2
   for j = 0 to 2
      array[i][j] = the next character

You have a number of ways to do "the next character", eg have a counter that tracks which is the next character, and increment it each time you use it

plaintext.charAt(counter ++ );
Member Avatar
mvmalderen
Nearly a Posting Maven
2,370 posts since Feb 2009
Reputation Points: 2,071 [?]
Q&As Helped to Solve: 282 [?]
Skill Endorsements: 40 [?]
 
0
 

while(plaintext.length() > 0) Strings are immutable, and the reference is never set to anything else. This has the potential to cause an infinite loop. The reason why it doesn't at runtime is because you keep incrementing counter, and accessing characters from that String by invoking plaintext.charAt(counter), eventually it will lead to a java.lang.StringIndexOutOfBoundsException, and since it is never caught, will terminate your program right away.

Another thing you should pay attention to is the scope in which you define your variables, take a look at these lines taken from the code you posted:

while (plaintext.length() > 0)
{
    ...
    ArrayList mylist = new ArrayList();
    mylist.add(text);
    ...
}

There are several things wrong about this, first off the scope: each loop iteration you'll create a new ArrayList instance, and it will only have one element in it. Next iteration the reference to the list goes out of scope, and the ArrayList you instantiated becomes eligible for Garbage Collection. The obvious fix is to create one ArrayList instance and keep adding characters to that list. You achieve this by moving your ArrayList mylist = new ArrayList();-line out of the loop. Like this:

ArrayList<Character> mylist = new ArrayList<Character>();
while (plaintext.length() > 0)
{
    ...
    mylist.add(text);
    ...
}

Also notice that I've added the type argument Character between angle brackets. If you don't, your compiler will generate a warning. However, you shouldn't ignore warnings!
The difference between ArrayList mylist = new ArrayList(); and ArrayList<Character> mylist = new ArrayList<Character>(); is that the second one allows the compiler to check that you only add Characters to the list. The first one would be analog to specifying Object as type argument: ArrayList<Object> mylist = new ArrayList<Object>(); which is not a good idea since the compiler won't be able to prevent you from making mistakes: in an ArrayList of Objects (that is the same as the ArrayList you get when you don't specify a type argument), you can add any (!) Object, so the compiler will not tell you if you accidentally add something to the list that is not a Character.

Also since it seems like you want to get all characters from a String, you might want to take a look at the toCharArray() method of the String class.
Example: char[] charsFromString = plaintext.toCharArray(); and follow what JamesCherrill suggested.

However I would like to suggest another approach since creating a List or char[] for the sole purpose of iterating over it, and discarding it is waste of resources (in this case) in my opinion. I came up with the following algorithm:

CONVERT-STRING-TO-2D-CHAR-ARRAY(S, XDIM, YDIM):
    Allocate an array R with dimensions [YDIM , XDIM]
    for i = 0 to S.length do
        R[i / XDIM][i % XDIM] = S.charAt(i)
    return R

[EDIT]You'll want to add a check i < XDIM * YDIM[/EDIT]

That is when you need a char[][] array, in case you only need the abstraction you could also decide to create a class that provides a 2D-'view' on a String by translating [x,y] values to their corresponding index in the String.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: