0% found this document useful (0 votes)
30 views14 pages

Arrays: Data Structures

The document discusses arrays and their use in representing polynomials. It provides two options for representing a polynomial using arrays: 1. Using a single array with a maximum degree limit, where terms are stored in the array indexes corresponding to their exponents. This wastes space if the actual degree is much lower. 2. Representing each term as a structure with exponent and coefficient fields, and storing all terms in a single global array. This allows polynomials of any degree but uses more space. The document also compares arrays to ArrayLists, noting arrays have a fixed size while ArrayLists are dynamically sized. It provides examples of adding two polynomials represented using these array options.

Uploaded by

林昱兼
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views14 pages

Arrays: Data Structures

The document discusses arrays and their use in representing polynomials. It provides two options for representing a polynomial using arrays: 1. Using a single array with a maximum degree limit, where terms are stored in the array indexes corresponding to their exponents. This wastes space if the actual degree is much lower. 2. Representing each term as a structure with exponent and coefficient fields, and storing all terms in a single global array. This allows polynomials of any degree but uses more space. The document also compares arrays to ArrayLists, noting arrays have a fixed size while ArrayLists are dynamically sized. It provides examples of adding two polynomials represented using these array options.

Uploaded by

林昱兼
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Arrays

Data Structures

Ching-Fang Hsu
Dept. of Computer Science and Information Engineering
National Cheng-Kung University
What is “Array”?

 Definition: An array is a set of pairs, <index,


value>, such that each index that is defined has
a value associated with it.
 A correspondence or a mapping
 Either a heterogeneous or a homogeneous
aggregate of data elements is valid in Java.

2
public class ArrayExample1 {
public static void main(String[] argv) {
int[] x;
x = new int[20];
for (int i = 0; i < x.length; i++) {
x[i] = 20 - i;
}
final variable
}
}
public class ArrayExample2 {
public static void main(String[] args) { homogeneous
int[] a1 = { 1, 2, 3, 4, 5 };
Object[] a2 = new Object[] {
new Integer(47), new Long(10), new Float(3.14),
new Double(11.11);
}
3
}
heterogeneous
Array vs. ArrayList in Java
Explicitly specified size

 Array: Simple fixed sized arrays The type of elements in


int int_arr[] = new int[10]; ArrayList to be created

 ArrayList: Dynamic sized arrays


ArrayList<Type> arr_L = new ArrayList<Type>();
Need not to
 Accessing and modifying elements
specify size
 Using [] vs. a set of methods

int_arr[0] = 10; ArrayList<Integer> arr_L = new


int_arr[1] = 9; ArrayList<Integer>(10);
System.out.println(int_arr[0]) arr_L.add(10);
; arr_L.add(9);
System.out.println(arrL.get(0)); 4
Array vs. ArrayList in Java
(contd.)
 The constraint on elements
 Array: Both primitive data types and objects of a class can be entries.
 ArrayList: Only objects are allowed!
ArrayList<int> intarr_L = new ArrayList<int>();
ArrayList<Integer> intarr_L = new ArrayList<Integer>();

5
Use Case 1: Ordered Lists

 Operations on ordered lists


 Length determination
 Scanning from left to right
 Item value retrival/setting
 Item insertion
 Item deletion
 Representing an ordered list as an array
 Associate itemi with the array index i.
 Sequential mapping

6
Use Case 1: Ordered Lists
(contd.)
 Sequential mapping works well for most
operations on ordered lists in constant time,
except insertion and deletion.
 Data movement

7
Example --- Polynomials

 ADT

8
Two non-zero polynomials
public Polynomial Add(Polynomial p1, Polynomial p2) {

Example: Polynomials (contd.)


int sum;
Polynomial d = Zero();
while(!IsZero(p1) && !IsZero(p2)) {
Comparison between two leading terms
Output and remove b’s
switch(COMPARE(LeadExp(p1), LeadExp(p2))){ leading term
case -1:
d = Attach(d, Coef(p2, LeadExp(p2)), LeadExp(p2).intValue());
p2 = Remove(p2, LeadExp(p2));
break;
case 0:
 For simplifying operations, exponents are
sum = Coef(p1, LeadExp(p1)).intValue() + Coef(p2, LeadExp(p2)).intValue();
if(sum != 0) {
arranged in decreasing order.
}
Attach(d, sum, LeadExp(p1));

p1 = Remove(p1, LeadExp(p1));
 Operation Add
p2 = Remove(p2, LeadExp(p2));
break;
Comparing terms from the two polynomials until one or
case 1:
d = Attach(d, Coef(p1, LeadExp(p1)), LeadExp(p1).intValue());
bothp1of= the polynomials
Remove(p1, becomes empty
LeadExp(p1));
break;
} Append the
}
polynomial with
for(; IsZero(p1); p1 = Remove(p1, LeadExp(p1))) { remaining terms
d = Attach(d, Coef(p1, LeadExp(p1)), LeadExp(p1));
}
for(; IsZero(p2); p2 = Remove(p2, LeadExp(p2))) {
d = Attach(d, Coef(p2, LeadExp(p2)), LeadExp(p2));
}
return d;
}
9
package array;

public class Struct1 {


private static final int MAX_DEGREE = 101;

Example: Polynomials --- int degree;


float[] coef = new float[MAX_DEGREE];

Representation public Struct1() {

 Option 1 public int returnMaxDegree() {


return MAX_DEGREE;
}
 Maximum degree is restricted by MAX_DEGREE.
public void setDegree(int d) {
 Struct1 degree = d;
}
 If a is of type polynomial and n < MAX_DEGREE,
public void setCoef(float[] c, int l) {
can be represented as for(int i = 0; i < l; i++) {
coef[i] = c[i];
a.setDegree(n);}
a.setCoef(i)
} = an-i, 0  i  n
public int getDegree() {
return degree;
}

public float getCoef(int i) {


return coef[i];
}
}
10
Example: Polynomials ---
Representation (contd.)
 The main drawback : lower flexibility on space
requirement
 Wasting a lot of space when the degree of the
polynomial is much less than MAX_DEGREE or the
polynomial is sparse

11
Example: Polynomials ---
package array;

Representation (contd.)public class Struct2 {

int expon;
float coef;
 Option 2 public Struct2() {
 Representing ai xi as
} a structure
 (Optional) Using only onevoid
public global array of this
setExpon(int e) { structure to
store all terms of a polynomial
expon = e;
}
 Struct2
public void setCoef(float c) {
coef = c;
}

public int getExpon() {


return expon;
}

public float getCoef() {


return coef;
}
}

12
The Polynomial Abstract Data
Type – Representation (contd.)
A(x) = 2x1000 + 1 B(x) = x4 + 10x3 + 3x2 + 1

startA finishA startB finishB avail

coef 2 1 1 10 3 1

expon 1000 0 4 3 2 0

0 1 2 3 4 5 6
2x1000
13
The Polynomial Abstract Data
Type – Representation (Contd.)
 No limit on the number of polynomial terms and
the maximum degree stored in a polynomial if
arraylists are used.
 The main drawback: About twice as much
space as option 1 is needed when all the terms
are nonzero.

14

You might also like