[操作疑難] 新手學JAVA, 睇書題目唔明有野問(array)

小弟剛剛學JAVA, 睇書想試下做佢後面D exercise, 唔係好知問咩

public void expand(Object a) {
// assume element has enough capacity
for (int i = size - 1; i >= 0; i--) {
element[4 * i + 3] = a;
element[4 * i + 2] = a;
element[4 * i + 1] = a;
element[4 * i] = element[i];
}
size = 4 * size;
}

element is a one-dimensional array that stores elements of the type Object. The data member size is such that the list elements are in positions 0 through size-1 of the array.

Question :
If the list element[0:2] = [9, 8, 7], whose size is 3, write the result and the size of the array element after executing
      expand(new Integer(18))
Assume that the array element has enough capacity for expansion.

同埋想問問個programme應該係點run? 小弟唔識答求教

提示: 作者被禁止或刪除 內容自動屏蔽

TOP

本帖最後由 rabbit82047 於 2013-10-17 00:09 編輯

條題目唔太難, 試下解, 可能仲難明

有條 array index 0 ~ 2 既值係 [9,8,7], size 係 3 < 呢個唔係 array 既實際 length
因為題目假設左 array 有足夠 capacity 做 expand
所以你可以當 size 係 limiting factor

題目就係要你 dry run 一次 expand(new Integer(18))
列出 expand 之後既 array index 0~size-1 所有數值同 [size]

TOP

我想問 size of array element係咪12,
expand (new Integer(18))  入面既數值有咩用
同埋我唔係好知條program應該點run
個flow應該係點?

TOP

條題目唔太難, 試下解, 可能仲難明

有條 array index 0 ~ 2 既值係 [9,8,7], size 係 3 < 呢個唔係 ...
rabbit82047 發表於 2013-10-17 00:07



    請問聲execute個flow應該係點

TOP

e d 要落手落腳post,幾可見化化哥浪費時間出手

TOP

本帖最後由 rabbit82047 於 2013-10-17 01:01 編輯

回復 5# 刀仔刀刀神


Flow 咪就係上至下有 loop 就 loop

其實你攞紙筆出黎 mark 低所有 variable 既值, array, i, size
一步步咁做,自然就有答案,完左再寫個 program 出黎驗證就得

expand(new Integer(18)) < 就係 create 一個 Integer object 值係 18 再 pass 入去 expand

其實你學到邊, 寫過 hello world 未?
知唔知 method, parameter, primitive data type, object 係咩意思?

要訓,聽日先會睇返

TOP

[11] = 18
[10] = 18
[9] = 18
[8] = 7
[7] = 18
[6] = 18
[5] = 18
[4] = 8
[3] = 18
[2] = 18
[1] = 18
[0] = 9

點解我整到呢堆野出黎

TOP

本帖最後由 rabbit82047 於 2013-10-17 08:02 編輯

既然你已經有答案, 試下 run 下面個 class
  1. import java.util.Arrays;

  2. public class Example {
  3.         private Object[] element;
  4.         private int size;
  5.        
  6.         public Example() {
  7.                 element = new Object[12];
  8.                 element[0] = new Integer(9);
  9.                 element[1] = new Integer(8);
  10.                 element[2] = new Integer(7);
  11.                
  12.                 size = 3;
  13.         }
  14.        
  15.         public void printArray() {
  16.                 System.out.printf("Current size is %d\nArray content: %s", size, Arrays.toString(element));
  17.         }
  18.        
  19.         public void expand(Object a) {
  20.                 // assume element has enough capacity
  21.                 for (int i = size - 1; i >= 0; i--) {
  22.                         element[4 * i + 3] = a;
  23.                         element[4 * i + 2] = a;
  24.                         element[4 * i + 1] = a;
  25.                         element[4 * i] = element[i];
  26.                 }
  27.                 size = 4 * size;
  28.         }
  29.        
  30.         public static void main(String[] args) {
  31.                 Example example = new Example();
  32.                
  33.                 example.expand(new Integer(18));
  34.                
  35.                 example.printArray();
  36.         }
  37. }
複製代碼

TOP

既然你已經有答案, 試下 run 下面個 class


THANK YOU!! 我走去問人, 人地仲話個SIZE 冇可能係12..
但係其實第28行句 size =4 *size;
有咩作用??

TOP