Reputation: 456
I have this piece of code:
int[][] pattern = new int[][]{
{ 1, 1, 1, 1, 1, 1, 1 },
{ 1, 2, 0, 0, 0, 2, 1 },
{ 1, 0, 3, 0, 3, 0, 1 },
{ 1, 0, 0, 4, 0, 0, 1 },
{ 1, 0, 3, 0, 3, 0, 1 },
{ 1, 2, 0, 0, 0, 2, 1 },
{ 1, 1, 1, 1, 1, 1, 1 },
};
I need to get this 2d array into a 2d ArrayList so i can manipulate it by adding rows and columns to move the pattern around. For example when my method calls for a shift of 2 rows and 2 columns i will be able to move the pattern to something like this:
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 1, 1, 1, 1, 1, 1, 1 },
{ 0, 0, 1, 2, 0, 0, 0, 2, 1 },
{ 0, 0, 1, 0, 3, 0, 3, 0, 1 },
{ 0, 0, 1, 0, 0, 4, 0, 0, 1 },
{ 0, 0, 1, 0, 3, 0, 3, 0, 1 },
{ 0, 0, 1, 2, 0, 0, 0, 2, 1 },
{ 0, 0, 1, 1, 1, 1, 1, 1, 1 },
I'm just looking to get the 2d array into a 2d Arraylist any help will be greatly appreciated!
Upvotes: 7
Views: 9282
Reputation: 55
For short, for 1d primitive int array, have to use IntStream.of() rather than stream() in this case
int[][] input = new int[][]{{1, 3, 10}, {2, 4, 55}};
System.out.println(
Arrays.stream(input)
.map(e -> IntStream.of(e).boxed().collect(Collectors.toList()))
.collect(Collectors.toList())
);
// [[1, 3, 10], [2, 4, 55]]
for boxed Integer array, see Daria Yu's answer
Upvotes: 1
Reputation: 2015
If you covert the primitive type int to reference type Integer, you can use stream:
public static void Two_Array_to_list_1() {
Integer[][] dataSet = new Integer[][] {{1, 2}, {3, 4}, {5, 6}};
List<List<Integer>> list =
Arrays.stream(dataSet).map(Arrays::asList).collect(Collectors.toList());
System.out.println(list);
}
Upvotes: 1
Reputation: 14228
You can do something like this:
public static List<Integer[]> twoDArrayList(int shift, int[][] input)
{
List<Integer[]> output = new ArrayList<Integer[]>();
if( input.length == 0 ) return null;
int columnlength = input.length;
int rowlength = input[0].length;
if (columnlength != rowlength) return null;
int padsize = shift;
for( int i = 0; i < padsize; i++ )
{
Integer[] zeroes = new Integer[shift+columnlength];
for( int j = 0; j < shift+columnlength; j++)
{
zeroes[j] = 0;
}
output.add( zeroes );
}
for( int i = 0; i < columnlength; i++ )
{
int[] row = input[i];
int[] zeroes = new int[shift];
List<Integer> temp = new ArrayList<Integer>();
for( int j = 0; j < shift; j++)
{
temp.add(0);
}
for( int k = 0; k < row.length; k++)
{
temp.add(row[k]);
}
output.add(temp.toArray(new Integer[]{}));
}
return output;
}
See demo here
When you supply shift as 2 : The output will look like:
Running Shifting array...
Array no. 0 in the list is : 0 0 0 0 0 0 0 0 0
Array no. 1 in the list is : 0 0 0 0 0 0 0 0 0
Array no. 2 in the list is : 0 0 1 1 1 1 1 1 1
Array no. 3 in the list is : 0 0 1 2 0 0 0 2 1
Array no. 4 in the list is : 0 0 1 0 3 0 3 0 1
Array no. 5 in the list is : 0 0 1 0 0 4 0 0 1
Array no. 6 in the list is : 0 0 1 0 3 0 3 0 1
Array no. 7 in the list is : 0 0 1 2 0 0 0 2 1
Array no. 8 in the list is : 0 0 1 1 1 1 1 1 1
When you supply 3 , your output looks like :
Running Shifting array...
Array no. 0 in the list is : 0 0 0 0 0 0 0 0 0 0
Array no. 1 in the list is : 0 0 0 0 0 0 0 0 0 0
Array no. 2 in the list is : 0 0 0 0 0 0 0 0 0 0
Array no. 3 in the list is : 0 0 0 1 1 1 1 1 1 1
Array no. 4 in the list is : 0 0 0 1 2 0 0 0 2 1
Array no. 5 in the list is : 0 0 0 1 0 3 0 3 0 1
Array no. 6 in the list is : 0 0 0 1 0 0 4 0 0 1
Array no. 7 in the list is : 0 0 0 1 0 3 0 3 0 1
Array no. 8 in the list is : 0 0 0 1 2 0 0 0 2 1
Array no. 9 in the list is : 0 0 0 1 1 1 1 1 1 1
Upvotes: 0
Reputation: 6079
Case 1 It is short, but need to covert the primitive type to reference type (int
to Integer
) as needed for Arrays.asList();
Integer[][] pattern = new Integer[][]{
{ 1, 1, 1, 1, 1, 1, 1 },
{ 1, 2, 0, 0, 0, 2, 1 },
{ 1, 0, 3, 0, 3, 0, 1 },
{ 1, 0, 0, 4, 0, 0, 1 },
{ 1, 0, 3, 0, 3, 0, 1 },
{ 1, 2, 0, 0, 0, 2, 1 },
{ 1, 1, 1, 1, 1, 1, 1 },
};
List<List<Integer>> lists = new ArrayList<>();
for (Integer[] ints : pattern) {
lists.add(Arrays.asList(ints));
}
Case 2 If you don't want to covert the primitive type to reference type: (int[][] pattern = new int[][]
to Integer[][] pattern = new Integer[][]
)
List<List<Integer>> lists = new ArrayList<>();
for (int[] ints : pattern) {
List<Integer> list = new ArrayList<>();
for (int i : ints) {
list.add(i);
}
lists.add(list);
}
Upvotes: 12