2012-02-01 2 views
0

, 정수 교체. if arrayList.get(i).get(j) == 3이면 2 등이되지만, 1 인 경우 1 인 경우에만 해당합니다. ARRAYLIST<ARRAYLIST<INTEGER>>의 특정 열에서만 가능합니다.다차원 ArrayList를하고, 그렇지 않으면 당신은 그것에서 <code>1</code>을 빼기 이중 차원 <code>ArrayList<ArrayList<Integer>></code>을 통해 당신이 그것을두고 <code>1</code> 같다 모든 <code>Int</code>가는 최고의 <em>(또는 어쨌든, 정말)</em> 무엇

답변

4

삽을 만드십시오. 한 가지 방법 만 있습니다. 모든 행의 모든 ​​열 반복 :

// example declaration only - initially all zeros until you set them. 
// assumes nrows and ncols are initialized and declared elsewhere 
int [][] matrix = new int[nrows][ncols]; 
for (int i = 0; i < matrix.length; ++i) { 
    for (int j = 0; j < matrix[i].length; ++j) { 
     // operate on the values here. 
     if (matrix[i][j] != 1) { 
      matrix[i][j] -= 1; 
     } 
    } 
} 

을 당신이 다음과 같습니다 목록의 목록을 가지고있는 경우에 :

List<List<Integer>> matrix = new ArrayList<List<Integer>>(); 
List<Integer> columnIdsToTransform = Arrays.asList({0, 4, 6 }); 
// You have to initialize the references; all are null right now. 
for (List<Integer> row : matrix) { 
    for (int j = 0; j < row.size(); ++j) {    
     // operate on the values here. 
     value = row.get(j); 
     if (columnsIdsToTransform.contains(j) && (value != 1)) { 
      row.set(value-1, j); 
     } 
    } 
} 

UPDATE : 당신의 편집에

을 바탕으로, 당신 이 변환을 수행 할 배열 또는 열 목록을 추가해야합니다. 두 번째 스 니펫에 예제를 추가했습니다.

+0

죄송합니다. 내가 게시했을 때 분명히 뇌 정신이 있었기 때문에 duffymo가 질문을 편집했습니다. –

1

모든 값을 반복하고 조건문을 사용하여 true 건에서 연산을 수행하십시오.

for (int i=0;i<Arraylist.size();i++) { 
    for (int j=0;j<Arraylist[i].size();j++) {  
     if (arrayList.get(i).get(j) != 1) 
      arrayList.get(i).get(j) -= 1;   
    } 
} 
관련 문제