小编sud*_*cat的帖子

如何遍历对象的ArrayList?

我有一个名为SparseMatrix的类.它包含节点的ArrayList(也是类).我想知道如何遍历数组并访问Node中的值.我尝试过以下方法:

 //Assume that the member variables in SparseMatrix and Node are fully defined.
 class SparseMatrix {
     ArrayList filled_data_ = new ArrayList();
     //Constructor, setter (both work)

     // The problem is that I seem to not be allowed to use the operator[] on
     // this type of array.
     int get (int row, int column) {
         for (int i = 0; i < filled_data_.size(); i++){
             if (row * max_row + column == filled_data[i].getLocation()) {
                 return filled_data[i].getSize();
             }
         }
         return defualt_value_;
     }
 }
Run Code Online (Sandbox Code Playgroud)

我可能会切换到静态数组(并在每次添加对象时重新创建它).如果有人有解决方案,我非常感谢您与我分享.另外,谢谢你提前帮助我. …

java arraylist

5
推荐指数
1
解决办法
2万
查看次数

我如何优化*这个?

我的代码提取是

if (num == 0) {
    cout << 1;
    cout << 2;
}
else if (num == 1) {
    cout << 0;
    cout << 2;
}
else if (num == 2) {
    cout << 0;
    cout << 1;
}
Run Code Online (Sandbox Code Playgroud)

其中0 <= num <= 2.

我在问,因为这些cout << ...陈述将被转换成更大的东西,但(几乎)彼此相同.

*我的意思是优化为'美化'代码(例如删除5个重复的cout语句).

我没有在问题中加入"美化",因为它在SO问题中听起来很奇怪.

c++ optimization

-1
推荐指数
1
解决办法
92
查看次数

标签 统计

arraylist ×1

c++ ×1

java ×1

optimization ×1