posts - 13, comments - 7, trackbacks - 0, articles - 0
          旋轉(zhuǎn)數(shù)組的基本思想就是左移數(shù)組,但是我們需要保存”相對的首位置”,并將其值插入到最后位置

          public class Test {   
               private char[] chArr;
               private int size;
               public Test(int size) {
                chArr = new char[size];
                size = 0;
               }
           
               //旋轉(zhuǎn)數(shù)組
               public void rotate(int newSize) {//newSize為要旋轉(zhuǎn)數(shù)組的大小,從右邊算起
                   int p = size - newSize;    //首位置
                    int i;
                    char t = chArr[p];
                    for (i = p+1; i < newSize; ++i) {//左移
                        chArr[i-1] = chArr[i];
                    }
                    chArr[i-1] = t;//將首位置的值插入到最后
              }
           
              public void display() {
                  for (int i = 0; i < chArr.length; ++i) {
                     System.out.print(chArr[i] + " ");
                  }
               }
           
              //向數(shù)組插入值
               public void insert(char c) {
                   chArr[size++] = c;
               }

               //測試
               public static void main(String[] args) {
                  Test test = new Test(3);
                  test.insert('c');
                  test.insert('a');
                  test.insert('t');
                  test.rotate(3);
                  test.display();
              }
          }//end test

          結(jié)果應(yīng)該為:"a t c"

          Feedback

          # re: 旋轉(zhuǎn)數(shù)組或是數(shù)組的部分值  回復(fù)  更多評論   

          2010-01-11 19:32 by zxy
          每次都遍歷 影響效率

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 正阳县| 报价| 临沧市| 清涧县| 德保县| 大埔区| 娄底市| 宝兴县| 五家渠市| 正宁县| 常山县| 象山县| 仙桃市| 萍乡市| 罗山县| 临西县| 昌黎县| 儋州市| 景德镇市| 浮山县| 西盟| 通州市| 德格县| 涿州市| 信宜市| 白沙| 洞头县| 中牟县| 河津市| 蒙城县| 越西县| 乳源| 正镶白旗| 西峡县| 巫溪县| 衢州市| 泌阳县| 云霄县| 高淳县| 二连浩特市| 新建县|