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ù)組的部分值  回復  更多評論   

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

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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 江门市| 白河县| 九台市| 深水埗区| 新绛县| 哈尔滨市| 台前县| 龙口市| 临泽县| 安庆市| 玉林市| 井研县| 龙胜| 行唐县| 建宁县| 舒城县| 山阳县| 临夏市| 白河县| 河北区| 弥渡县| 泸州市| 湘潭县| 漳浦县| 务川| 贵德县| 普宁市| 塔河县| 黔东| 西峡县| 桐乡市| 曲松县| 肇东市| 镇坪县| 新巴尔虎右旗| 中牟县| 乐都县| 怀来县| 交口县| 河南省| 华容县|