package pre3; import java.util.ArrayList; /** * Some utilities for working with
ID: 3532530 • Letter: P
Question
package pre3;
import java.util.ArrayList;
/**
* Some utilities for working with arrays, with particular
* relevance to homework 3.
*/
public class ArrayUtil
{
/**
* Remove an element at index pos from the array. All elements to the right of
* the given position are shifted down, and the last cell of the array is
* filled with a zero. For example, if arr = [1, 10, 3, 5, 7], after invoking
* remove(arr, 2), arr should be [1, 10, 5, 7, 0] (Section 7.6 in the textbook
* may have some useful advice.)
*
* @param arr
* the array from which to remove an element
* @param pos
* the position at which the element should be removed
*/
public static void remove(int[] arr, int pos)
{
}
/**
* Remove an element at index pos from the array. All elements to the left of
* the given position are shifted up, and the first cell of the array is
* filled with a zero. For example, if arr = [1, 10, 3, 5, 7], after invoking
* removeAndShiftUp(arr, 2), arr should be [0, 1, 10, 5, 7] (Section 7.6 in
* the textbook may have some useful advice.)
*
* @param arr
* the array from which to remove an element
* @param pos
* the position at which the element should be removed
*/
public static void removeAndShiftUp(int[] arr, int pos)
{
// TODO
}
/**
* Removes all negative (less than 0) elements from the array. You must
* maintain the order of all the remaining elements in the array and shift
* them down, and pad the array with zeros. For example, if arr = [1, -10, 3,
* -5, 7], after invoking removeNegatives(arr), arr should become [1, 3, 7, 0,
* 0].
*
* @param arr
* the array from which to remove the negative elements
*/
public static void removeNegatives(int[] arr)
{
// TODO
}
/**
*
* Finds the runs in the given array and returns a list of the indices of all
* elements that are in a run, where a run is defined as a sequence of three
* or more adjacent repeated values. The indices are returned in ascending
* order. For example, if arr = [6, 2, 5, 5, 5, 5, 4, 4, 5, 2, 2, 2, 1], then
* arr contains two runs, 5 5 5 5 and 2 2 2, and the method would return the
* indices [2, 3, 4, 5, 9, 10, 11].
*
* @param arr
* the array in which to find the runs
* @return a list of indices of all elements that are part of a run, in
* ascending order
*/
public static ArrayList<Integer> findRuns(int[] arr)
{
// TODO
return null;
}
/**
* Removes all the runs in an array and then pads to the end of array with
* zeros, where a run is defined as a sequence of three or more adjacent
* repeated values. For example, if arr = [6, 2, 5, 5, 5, 5, 4, 4, 5, 2, 2, 2,
* 1], then arr contains two runs, 5 5 5 5 and 2 2 2, and after invoking
* removeRuns(arr) the resulting array would be [6, 2, 4, 4, 5, 1, 0, 0, 0, 0,
* 0, 0, 0]
*
* @param arr
* the array from which to remove the runs
*/
public static void removeRuns(int[] arr)
{
// TODO
}
/**
* Exchanges the elements of one row with those in a different row in a given
* 2D array. Each element remains in the same column. Assume that all rows are
* the same length.
*
* @param arr
* the array whose rows should be exchanged
* @param row1
* one of the rows to swap
* @param row2
* other row to swap
*/
public static void swapRows(int[][] arr, int row1, int row2)
{
// TODO
}
/**
* Exchanges the elements of one column with those in a different column in a
* given 2D array. Each element remains in the same row. Assume that all rows
* are the same length.
*
* @param arr
* the array whose columns are to be exchanged
* @param col1
* one of the columns to swap
* @param col2
* other column to swap
*/
public static void swapColumns(int[][] arr, int col1, int col2)
{
// TODO
}
// The methods below may be useful for debugging, you do not need to modify them.
/**
* Returns a string representation of the given array. This method is
* essentially the same as java.util.Arrays.toString(int[] arr).
*
* @param arr
* the array
* @return
* a string representation of the given array
*/
public static String toString(int[] arr)
{
if (arr.length == 0)
{
return "[]";
}
String ret = "[" + arr[0];
for (int row = 1; row < arr.length; ++row)
{
ret += ", " + arr[row];
}
ret += "]";
return ret;
}
/**
* Returns a string representation of the given 2D array.
*
* @param arr
* the array
* @return
* a string representation of the given array
*/
public static String toString(int[][] arr)
{
if (arr.length == 0)
{
return "[]";
}
String ret = "[" + toString(arr[0]);
for (int row = 1; row < arr.length; ++row)
{
ret += ", " + toString(arr[row]);
}
ret += "]";
return ret;
}
}
Explanation / Answer
Hi, I have added two additional methods to swap rows and cols, named swapRows() and swapCols()
Here you go :
import java.util.ArrayList;
import java.util.Set;
import java.util.TreeSet;
/**
* Some utilities for working with arrays, with particular
* relevance to homework 3.
*/
public class ArrayUtil
{
/**
* Remove an element at index pos from the array. All elements to the right of
* the given position are shifted down, and the last cell of the array is
* filled with a zero. For example, if arr = [1, 10, 3, 5, 7], after invoking
* remove(arr, 2), arr should be [1, 10, 5, 7, 0] (Section 7.6 in the textbook
* may have some useful advice.)
*
* @param arr
* the array from which to remove an element
* @param pos
* the position at which the element should be removed
*/
public static void remove(int[] arr, int pos)
{
for(int i=pos;i<(arr.length-1);i++)
{
arr[i]=arr[i+1];
}
arr[arr.length-1]=0;
}
/**
* Remove an element at index pos from the array. All elements to the left of
* the given position are shifted up, and the first cell of the array is
* filled with a zero. For example, if arr = [1, 10, 3, 5, 7], after invoking
* removeAndShiftUp(arr, 2), arr should be [0, 1, 10, 5, 7] (Section 7.6 in
* the textbook may have some useful advice.)
*
* @param arr
* the array from which to remove an element
* @param pos
* the position at which the element should be removed
*/
public static void removeAndShiftUp(int[] arr, int pos)
{
for(int i=pos;i<0;i--)
{
arr[i]=arr[i-1];
}
arr[0]=0;
}
/**
* Removes all negative (less than 0) elements from the array. You must
* maintain the order of all the remaining elements in the array and shift
* them down, and pad the array with zeros. For example, if arr = [1, -10, 3,
* -5, 7], after invoking removeNegatives(arr), arr should become [1, 3, 7, 0,
* 0].
*
* @param arr
* the array from which to remove the negative elements
*/
public static void removeNegatives(int[] arr)
{
for(int i=0;i<arr.length;i++)
{
if(arr[i]<0)
{
remove(arr, i);
}
}
}
/**
*
* Finds the runs in the given array and returns a list of the indices of all
* elements that are in a run, where a run is defined as a sequence of three
* or more adjacent repeated values. The indices are returned in ascending
* order. For example, if arr = [6, 2, 5, 5, 5, 5, 4, 4, 5, 2, 2, 2, 1], then
* arr contains two runs, 5 5 5 5 and 2 2 2, and the method would return the
* indices [2, 3, 4, 5, 9, 10, 11].
*
* @param arr
* the array in which to find the runs
* @return a list of indices of all elements that are part of a run, in
* ascending order
*/
public static ArrayList<Integer> findRuns(int[] arr)
{
Set<Integer> runInd=new TreeSet<Integer>();
for(int i=0;i<(arr.length-2);i++)
{
if(arr[i]==arr[i+1] && arr[i+1]==arr[i+2])
{
runInd.add(i+1);
runInd.add(i+2);
runInd.add(i+3);
}
}
return new ArrayList<Integer>(runInd);
}
/**
* Removes all the runs in an array and then pads to the end of array with
* zeros, where a run is defined as a sequence of three or more adjacent
* repeated values. For example, if arr = [6, 2, 5, 5, 5, 5, 4, 4, 5, 2, 2, 2,
* 1], then arr contains two runs, 5 5 5 5 and 2 2 2, and after invoking
* removeRuns(arr) the resulting array would be [6, 2, 4, 4, 5, 1, 0, 0, 0, 0,
* 0, 0, 0]
*
* @param arr
* the array from which to remove the runs
*/
public static void removeRuns(int[] arr)
{
ArrayList<Integer> list=findRuns(arr);
for(int e:list)
{
remove(arr, e);
}
}
/**
* Exchanges the elements of one row with those in a different row in a given
* 2D array. Each element remains in the same column. Assume that all rows are
* the same length.
*
* @param arr
* the array whose rows should be exchanged
* @param row1
* one of the rows to swap
* @param row2
* other row to swap
*/
public static void swapRows(int[][] arr, int row1, int row2)
{
int cols = arr[0].length;
for (int col=0; col<cols; col++)
swapRow(arr, row1, col, row2, col);
}
//Swap Row
private static void swapRow(int[][] a, int i0, int j0, int i1, int j1) {
int temp = a[i0][j0];
a[i0][j0] = a[i1][j1];
a[i1][j1] = temp;
}
/**
* Exchanges the elements of one column with those in a different column in a
* given 2D array. Each element remains in the same row. Assume that all rows
* are the same length.
*
* @param arr
* the array whose columns are to be exchanged
* @param col1
* one of the columns to swap
* @param col2
* other column to swap
*/
public static void swapColumns(int[][] arr, int col1, int col2)
{
int rows = arr.length;
for (int row=0; row<rows; row++)
swapCol(arr, row, col1, row, col2);
}
//Swap Cols
private static void swapCol(int[][] a, int i0, int j0, int i1, int j1) {
int temp = a[i0][j0];
a[i0][j0] = a[i1][j1];
a[i1][j1] = temp;
}
// The methods below may be useful for debugging, you do not need to modify them.
/**
* Returns a string representation of the given array. This method is
* essentially the same as java.util.Arrays.toString(int[] arr).
*
* @param arr
* the array
* @return
* a string representation of the given array
*/
public static String toString(int[] arr)
{
if (arr.length == 0)
{
return "[]";
}
String ret = "[" + arr[0];
for (int row = 1; row < arr.length; ++row)
{
ret += ", " + arr[row];
}
ret += "]";
return ret;
}
/**
* Returns a string representation of the given 2D array.
*
* @param arr
* the array
* @return
* a string representation of the given array
*/
public static String toString(int[][] arr)
{
if (arr.length == 0)
{
return "[]";
}
String ret = "[" + toString(arr[0]);
for (int row = 1; row < arr.length; ++row)
{
ret += ", " + toString(arr[row]);
}
ret += "]";
return ret;
}
}
Happy to help :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.