[DS: 3]Addressing & Array &Sorting

  • Basics in Computer Memory
    • ordered sequence of bytes
    • unique address
    • key difference between high & low level programming languages
      • whether programmer has to deal with memory addressing directly
    • Numbers
      • Binary
      • Octal (0,1,2,3,4,5,6,7)
      • Hexadecimal (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F)
    • Memory allocation
      • structure:

        • Q1 : Why is it designed like this?
          • A1: The memory used by local variables will be automatically released without additional memory management operations in the stack area.
        • see the structure in the other direction
          • When a new object is created, Java allocates space from heap.
          • When a method is called, Java allocates a new block of memory called a stack frame to hold its local variables.
          • When a method returns, its stack frame is erased. Stack frames come from stack
        • About Stack vs Heap
          • It is estimated that there will be other essays talking about each of them
    • Garbage Collection
      • When memory is running short, Java does garbage collection
        • Mark the objects referenced by variables on stack or in static storage.
        • Sweep all objects in the heap, reclaim unmarked objects (garbage)
  • Arrays
    • Features:
      • Ordered and fixed length
      • Homogeneous: Each value in the array is of the same type
    • An array is characterized by
      • ◦ Element type
      • ◦ Length
    • Default values in initialization
      • numerics 0
      • boolean false
      • objects null
    • Two-dimensional arrays:
      • Each element of an array is an array (of the same dimension)

    • Typical operations on an array
      • } Traversal: processing each element

      • } Search: finding an element by using the given index or key
      • } Insertion: adding new elements

      • } Deletion: removing elements

      • } Merging: combining two arrays into a single array
  • Implementation
    • Use named constant to declare the length of an array.
      • private static final int N_JUDGES = 5; double[] scores = new double[N_JUDGES];
    • Identifying an element by an integer number or an expression
      • array[index], array[(a+b)/2]
    • Cycling through array elements
      • Easier code attention

    • Two typical starting numbers of indexing
      • ◦ 0: Java, Python, C/C++, …
      • ◦ 1: Matlab, Julia, R, …
    • Passing Arrays as Parameters?
      • [DS:3.1]Passing objects (references) versus primitive type (values) as parameters.
      • [DS:3.1]Passing objects (references) versus primitive type (values) as parameters.
      • Pass-by-Value
        • ◦ copied to another location of the memory
          • ◦ accesses only the copy, no effect on the original value
          • change(i){ i=5;}
      • Pass-by-Reference
        • memory address is passed to that function, so the function changes the actual variable
        • example: stringBuffer.append
      • For primitive types
        • pass-by-value, which means a copy of the value is passed, and the original value is not changed.
      • For objects,
        • although the value passed is also a value (the memory address of the object, i.e., a reference), any modifications made to the object within the method will affect the original object because this value can be used to access and modify the object's state. This behavior is similar to pass-by-reference ​[DS:3.2]Understanding Java's Memory Management and Array Return Mechanism
          • but strictly speaking, Java does not have true pass-by-reference(the value of address instead of the referrence itself).
      • In Java, whether something remains unchanged or not depends on
        • whether the first one has been assigned an address
        • whether the second one has either been allocated a new address or is being pointed to the address of the first one.
        • The key factor is whether the addresses of the two are the same or not.

  • Examples
    • String Array:
      • String array is an array holding a fixed number of strings or string values
        • ◦ One structure commonly used in Java
        • ◦ Even the arguments of the Java ‘main’ function is a String Array public static void main(String args[ ]){…}
        • Each string is an object!
      • codes

    • Jagged Arrays
      • } Jagged arrays have rows of unequal length
        • ◦ each row has a different number of columns, or entries
      • } Jagged arrays are allowed in Java

  • Sorting Strategies ​[DS:3.3]Important! Sorting with arrays
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值