DS: Arrays

¡Supera tus tareas y exámenes ahora con Quizwiz!

SOLVE [Java Question] What will be the output of this program? public class ArraysInJava { static void methodOne(int[] a) { int[] b = new int[5]; a = b; System.out.print(a.length); System.out.print(b.length); } public static void main(String[] args) { int[] a = new int[10]; methodOne(a); System.out.print(a.length); } }

5510

[Java Question] What is two dimensional array?

An array of array in Java. You can declare them like int[][] primes = new int[3][3] which is a matrix of 3x3.

[Java Question] Where is an array stored in memory?

Array is created in heap space of JVM memory. Since array is object in Java, even if you create array locally inside a method or block, object is always allocated memory from heap.

What are arrays good for?

Arrays are good for indexed access and operations which involve swapping elements.

What's the complexity of deletion of sorted array element at end of array? At middle? At beginning?

End -- O(1) requires no extra work Middle -- O(n) requires moving elements Beginning -- O(n) requires moving elements

Declare an array in C

Ex: int myArray[5] = {1,2,3,4,5}

Declare an array in Python

Ex: myArray = [1,2,3,4,5]

What do arrays do?

Hold values under a single name

What is the complexity of insertion at the end of a given array?

If you are simply inserting to the last element of the array O(1). If you have an element in that position and need a resize or if you need to find that last open element (meaning you do not have a filled-1 array), O(n).

What is the most efficient way to assign or print out arrays?

Loops

Can an entire array be printed out at once?

No

[Java Question] Can you change size of array once created?

No, you cannot change the size of array once created. If you need dynamic array, consider using ArrayList class, which can resize itself.

What's the complexity of finding the exact middle index of an array?

O(1) -- finding size is constant, dividing is constant, lookup is constant

What is the complexity of insertion into any position other than the end of an array?

O(n) -- You must remember next element and replace it with previous element, repeat until all elements are replaced.

SOLVE An array contains n integers ranging from 0 to n-2. Since n > n-2, exactly one number is duplicated. How do you find which number is the duplicate?

One way to solve this is to add all the elements of the array and compare the expected result minus the real result. For example {0,1,2,3,3} result = 9 expected result = 10 (with 0-4 as elements). Duplicate is 3 since we are 1 away from expected result.

SOLVE How do you find the missing number in an integer array of 1 to 100? The elements at the index are equal, therefore element at index 1 is 1, 2 is 2, etc.

One way to solve this is to add all the elements of the array. If there were no elements missing . The answer is 5050 if all numbers are there (this is probably not necessary to know but just explain the process). Then 5050-result = missing number

SOLVE Reverse an array without affecting special characters.

Simple solution: 1) Create a temporary character array say temp[]. 2) Copy alphabetic characters from given array to temp[]. 3) Reverse temp[] using standard string reversal algorithm. 4) Now traverse input string and temp in a single loop. Wherever there is alphabetic character is input string, replace it with current character of temp[]. Complexity: O(n) More efficient way: 1) Let input string be 'str[]' and length of string be 'n' 2) l = 0, r = n-1 3) While l is smaller than r, do following a) If str[l] is not an alphabetic character, do l++ b) Else If str[r] is not an alphabetic character, do r-- c) Else swap str[l] and str[r]

Can an array hold different data types?

The simplest form of an array cannot

Can an array be used for a Hash table?

Yes -- simplest form would be to have your hash function rep an index.

Can an array be used for a binary tree?

Yes, where left child is 2 * parent position and right child is 2*parent position +1

[Java Question] Can you make array volatile in Java?

Yes, you can make an array volatile in Java, but you only make the variable which is pointing to array volatile. If array is changed by replacing individual elements than happens before guarantee provided by volatile variables will not held.


Conjuntos de estudio relacionados

SS3A - Quiz Compilation Study Guide

View Set

Medical Microbiology: Gram Positive Bacteria + Infections

View Set

Neuro. Chp. 37-1, 37-2, EXTRA EXTRA

View Set

identify the 6 characteristics of living things.

View Set

NUR 1024: NCLEX Questions for Osteoarthritis

View Set

2.5 Review - The Process Costing System

View Set

Western Europe in Early Middle Ages Review

View Set