從二維陣列中洗掉 col 幾乎沒有問題。
目標是從每一行中洗掉特定索引"2"并將其回傳。
它應該如何
我做到了,但0最后一點問題都沒有。
我是怎么得到的
private static void removeEntry(int[][] workArray, int col) {
int row = workArray.length;
//largest row count
int max = 0;
int tempNum = 0;
for (int[] ints : workArray) {
tempNum = 0;
for (int j = 0; j < ints.length; j ) {
tempNum ;
if (tempNum > max) {
max = tempNum;
}
}
}
int [][] newArray = new int[row][max];
for(int i = 0; i < row; i ) {
for(int j = 0; j < max; j ) {
if(j < col && j < workArray[i].length) {
newArray[i][j] = workArray[i][j];
}else if (j == col) {
// Do nothing
} else if (j > col && j < workArray[i].length) {
newArray[i][j - 1] = workArray[i][j];
}
}
}
for (int i = 0; i < workArray.length; i ) {
for (int j = 0; j < workArray[i].length; j ) {
workArray[i][j] = newArray[i][j];
}
}
然后我嘗試洗掉0但不作業
int remIndex = 0;
for (int i = 0; i < workArray.length; i ) {
for (int j = remIndex; j < workArray[i].length-1; j ) {
if(workArray[i][j] == remIndex){
workArray[i][j] = workArray[i][j 1];
}
}
}
uj5u.com熱心網友回復:
陣列是一個占據連續記憶體塊的資料容器,它的大小應該在實體化陣列時定義并且不能更改。
如果您需要一個更小或更大長度的陣列,則需要創建一個新陣列并復制所有以前添加的應保留的元素。
Java中也沒有“二維”陣列這樣的東西,我們可以創建一個嵌套陣列,即包含其他陣列的陣列。
與我們如何在普通陣列中的特定索引處重新分配整數值類似int[],我們可以更改陣列陣列中的參考,即我們可以使其指向另一個(新創建的)陣列。
這就是你的作業中需要做的事情,因為方法是void. 即,需要通過替換其所有“行”來更改給定陣列,這些“行”的長度大于或等于要洗掉的給定列 col的長度,新陣列將保留除 index 處的元素之外的所有先前元素col。
private static void removeEntry(int[][] workArray, int col) {
for (int row = 0; row < workArray.length; row ) {
if (workArray[row].length <= col) { // no need to change anything
continue;
}
int newLength = workArray[row].length - 1;
int[] newRow = new int[newLength]; // creating a new row shorter by 1
for (int oldCol = 0, newCol = 0; oldCol < workArray[row].length; oldCol , newCol ) {
if (oldCol == col) { // skipping the target column
newCol--; // newCol would be incremented automatically at the end of the iteration, but we want it to remain the same
continue;
}
newRow[newCol] = workArray[row][oldCol];
}
workArray[row] = newRow; // reassigning the row
}
}
main()
public static void main(String[] args) {
int[][] testArr =
{{1, 2},
{1, 2, 3},
{1, 2, 3, 4}};
removeEntry(testArr, 2);
// printing the testArr
for (int[] arr: testArr) {
System.out.println(Arrays.toString(arr));
}
}
輸出:
[1, 2]
[1, 2]
[1, 2, 4]
在線演示的鏈接
如果你把方法void弄錯了,你需要回傳一個新的陣列,即回傳型別int[](仔細檢查你的賦值要求)。然后需要對上面解釋和實作的邏輯進行一個小的更改:每個陣列都應該用一個新陣列替換,然后放入新創建的結果陣列中。
注意Arrays.copyOf()允許創建孔陣列的副本,并且System.arraycopy()可以幫助您將給定范圍內的元素從一個陣列復制到另一個陣列,但是因為您正在處理分配,我建議您使用回圈手動執行它,因為您應該展示有關如何操作陣列的知識,而不是特殊實用功能的知識(除非作業中另有說明)。
private static int[][] removeEntry(int[][] workArray, int col) {
int[][] result = new int[workArray.length][];
for (int row = 0; row < workArray.length; row ) {
int newLength = col < workArray[row].length ? workArray[row].length - 1 : workArray[row].length;
int[] newRow = new int[newLength];
for (int oldCol = 0, newCol = 0; oldCol < workArray[row].length; oldCol , newCol ) {
if (oldCol == col) {
newCol--;
continue;
}
newRow[newCol] = workArray[row][oldCol];
}
result[row] = newRow; // reassigning the row
}
return result;
}
main()
public static void main(String[] args) {
int[][] testArr =
{{1, 2},
{1, 2, 3},
{1, 2, 3, 4}};
int[][] newArr = removeEntry(testArr, 2);
// printing the testArr
for (int[] arr: newArr) {
System.out.println(Arrays.toString(arr));
}
}
輸出:
[1, 2]
[1, 2]
[1, 2, 4]
在線演示的鏈接
uj5u.com熱心網友回復:
這是我的方法。我曾經System.arraycopy()將陣列復制到洗掉的列,并從洗掉的列之后直接復制到末尾。這樣,我們就從陣列中完全洗掉了該列。
private static int[][] removeEntry(int[][] workArray, int col) {
int[][] resultArray = new int[workArray.length][];
int index = 0;
for (int[] row : workArray) {
if (row.length - 1 < col) {
resultArray[index] = row;
index ;
continue;
}
int[] arrayCopy = new int[row.length - 1];
System.arraycopy(row, 0, arrayCopy, 0, col);
System.arraycopy(row, col 1, arrayCopy, col, row.length - col - 1);
resultArray[index] = arrayCopy;
index ;
}
return resultArray;
}
uj5u.com熱心網友回復:
陣列是固定大小的。在初始化陣列時,其中存盤了一個默認值。這里,0 被存盤為默認值。所以我建議你不要將“col”初始化為“max”,而是在 for 回圈中這樣做:-
int [][] newArray = new int[row][];
for(int i = 0; i < row; i ) {
if(col <= workArray[i].length){
newArray[i] = new int[workArray[i].length-1]; //initialize it here
}
for(int j = 0; j < workArray[i].length; j ) {
if(j < col) {
newArray[i][j] = workArray[i][j];
}else if (j == col) {
// Do nothing
} else if (j > col) {
newArray[i][j - 1] = workArray[i][j];
}
}
}
return newArray;
您還應該回傳修改后的陣列,因為我們無法更改陣列的大小,因此我們必須創建一個新陣列 ( newArray)。因此,請將方法定義更改為:-
private static int[][] removeEntry(int[][] workArray, int col)
最后,整個方法看起來像:-
private static int[][] removeEntry(int[][] workArray, int col) {
int row = workArray.length;
//largest row count
int [][] newArray = new int[row][];
for(int i = 0; i < row; i ) {
if(col <= workArray[i].length){
newArray[i] = new int[workArray[i].length-1]; //initialize it here
}
for(int j = 0; j < workArray[i].length; j ) {
if(j < col) {
newArray[i][j] = workArray[i][j];
}else if (j == col) {
// Do nothing
} else if (j > col) {
newArray[i][j - 1] = workArray[i][j];
}
}
}
return newArray;
}
你可以像這樣使用它:-
workArray = removeEntry(workArray, 2);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/483019.html
