0

I'm seeking to place a 1 dimensional array inside of a 2d array with java. With the method I call I'm passing the list that I'll be collecting the data from and also the 2d array I want to pass the information to. After creating a set of an string I dissect the sentence by 2 place the characters into shingleSet. From shingleSet I must convert the strings to an array, sort the data then save the sorted array into the 2d array. The problem I'm having is that I can load the array but I can only load it locally. Would I have to return a 2d array from the method?

private static void sentenceToShingles(int n, String[][] stringArray, List list){
    int i, j;
    String e;
    String [] newShingle;
    int counter = 0;
    stringArray = new String [n][];
    for(Object o: list){
        //for every sentence create a new hashset
        Set<String> shingleSet = new HashSet();
        e = (String) o;
        i=0;
        while((i+2) < e.length()){
            for(j=0;j<2;j++){
               shingleSet.add(e.substring(i, i+2));  
            } 
            i++;      
        }    

        //add to array and sort ?RETURNING?
        newShingle = shingleSet.toArray(new String[shingleSet.size()]);
        Arrays.sort(newShingle);

        //?? How can I save newShingle into 2d array??

        //print shingle sorted shingle array
        for(j=0; j<newShingle.length; j++){
            System.out.print(newShingle[j] + " ");
        } 
       System.out.println();
    }        
    System.out.println();              
}
Abubakr Dar
  • 4,078
  • 4
  • 22
  • 28
Gnarl Marx
  • 1
  • 1
  • 2
  • This answer should help: [Answer1](http://stackoverflow.com/questions/5134555/how-to-convert-a-1d-array-to-2d-array) – vianna77 Feb 17 '15 at 15:03

1 Answers1

0

To return the array from the method change the method signature from private static void to private static String[][] The add a return statement at the end of your code: return stringArray;

Update

To fill a 2D array you need to loop over each row and column:

for(int row = 0; row < targetArray.length; row++) {
    for(int col = 0; col < targetArray[row].length; col++) {
        targetArray[row][col] = sourceArray[row];
    }
}
Travis Pettry
  • 1,220
  • 1
  • 14
  • 35
  • I'm aware that I would have to change the prefix of the method but my main question is how do I put the newShingle array inside of the 2d stringArray? – Gnarl Marx Feb 17 '15 at 15:01
  • Do you want your output array to look like [[000] [111] [222] [333]] But if the sets were stacked vertically. – Travis Pettry Feb 17 '15 at 15:07
  • I would need for them to look like [[set1 ] [set2] [set3]] each set is an array of sentences divided into 2 character sets – Gnarl Marx Feb 17 '15 at 15:18