Module 3: Questions and Answers

Some of our TAs have put together some short videos of answers to commonly asked questions from Module 3:

 

And here are some of questions that I found interesting and my answers:

Q. Is there a reason that arrays start at index 0 instead of 1?
A: Yes! 0 actually represent the "distance from the start" of the array. So the first item in the array is "0 items away from the start".   This is convenient because it can be used to efficiently retrieve data from memory (base plus index addressing Links to an external site.). You may have heard of computer memory being referred to as "RAM". RAM stands for "Random Access Memory Links to an external site." and the "random access" part is really about efficient access using an index. Arrays are a direct proxy for RAM.  In CSE247 you'll study ways to efficiently store and access data that rely on this "random access" property of arrays. If you take CSE132, 332, 361, 362, or 422 you'll probably look at details about why starting at 0 makes some things easier than they'd be otherwise. 

Q. Can you make an array that starts at index 1 instead of 0?
A. Not in Java. If 1-based indexing is really preferable you can just make an array with an extra location and ignore index 0. 

Other programming languages do sometimes start at 1. Mathematicians have historically used 1-based indexing when talking about mathematical constructs (matrices, series, etc.) and some programming languages used in math intensive fields use 1-based indexing. For example, Fortran, Matlab, and Mathematica are all widely used for scientific computing and all use indices that start at 1 by default. There are also languages that let you define the starting index (so it can be any integer) --- Fortran and Pascal allow you to pick the range of indices, so, for example, it's possible to have an array with indices from -2 to 4.


Q. Can an array change its size?
A. No. An array's size is fixed when you create it with "new". This is related to how memory is managed and being able to support the "Random Access" property mentioned above. That being said, there are "managed arrays" that can increase in size as-needed. Java has Vector and ArrayList objects that act like arrays (and are using arrays), but can be expanded (by replacing arrays with larger arrays). We'll cover the ArrayList later in the semester. 

 

Also, although an array can't change sizes, the variable that refers to the array can be changed to refer to another array. For example:

int[] myArray = new int[5];  // Make myArray refer to an array that can hold 5 ints
for(int i=0;i<myArray.length;i++) {
  myArray[i] = i;
}
// myArray is an array like:  {0,1,2,3,4}
myArray = new int[2];   // Now myArray refers to an new array that can hold 2 int (both are 0). 


Q. How often are arrays used in mobile applications?
A. Nearly all mobile apps make some use of a data structure called a Map (covered in CSE247). Maps often use an array, so almost all mobile apps use an array in some form.

 

If you plan to write mobile apps you may or may not use arrays directly in your code.  It really depends on the application.  There are many other approaches to storing data that are favored over simple arrays (those are also covered in 247). For example, Java's ArrayList class has the same strengths as arrays, like being able to efficiently use an index, but overcomes some of the limitations, like being unable to increase the array's length. ArrayList has the same strengths as arrays because it's actually using and managing arrays.

Q. Do 3D (or larger dimension) arrays exist?
A: Yes! They are really really useful. For example, if you were trying to study radiotherapy treatments for cancer, you may use a 3D array to represent a volume of space. Some of the "blocks" (voxels Links to an external site.) in the volume would represent parts of the patient's body: some would be the cancerous areas and others healthy tissue. You may simulate how radiation travels from some source and interacts with the different voxels. This would allow you to simulate different treatments to see their impact before actually treating a patient. 3D arrays are particularly useful for certain types of 3D simulation like this.

In Java (the language) there isn't really a limit to the number of dimensions allowed. There are some applications that use even higher dimensional matrices. In reality there may be some upper limit. For example, it looks like some compilers have limited arrays to ~255 dimensions. (See here Links to an external site.).  It's hard to envision the need for that many dimensions, but there would be ways to overcome the limitation. 

Q. Are arrays the same as Python's concept of Dictionaries?
A. No, Arrays are comparable to Python's Lists. Later this semester we'll talk about Java's Maps, which are comparable to Python's Dictionaries.

Q. Is it possible to do Matrix math in Java using Arrays?
A. Yes and no. You can't just use the arithmetic operators, like +, -, *, etc. with arrays. There are libraries that store data in arrays and let you do linear algebra/matrix math operations. 

Q. How are you? 
A: Thanks for asking. I'm good, but it was a long week so TGIF!  And you?

Q. Why are arrays necessary?
A. They serve several purposes. Here are a few:

  • storing data in simulations (as described above in the question about 3D arrays)
  • allowing programs to flexibly work with arbitrary amounts of data data. For example, if I wanted to write a program to act as a grade book for this course I could either: a) Create 625 String variables to represent each student's name or b) Create a single array that can hold 625 items (and read that data in from a file). The second choice is both less code and will also be "reusable" in future semesters that have more students --- I can just change the size of the array.
  • being a proxy for RAM they have the "random access" property, which is useful.


Q. What is the String[] args part in public static void main(String[] args) {...}
A. It's an array of String objects.  This allows the program to be run with "command line arguments" (args is short for arguments, as in the "argument" to a math function). You may have seen hacker-ish scenes in movies or on TV where someone runs programs on a computer by typing things in at a prompt. Java programs can be run in this way and we can "pass in" information for the program to use. For example:

public class PrintFromPrompt {
	public static void main(String[] args) {
		System.out.println("This program was run with the arguments:");
		for(String s: args) {
			System.out.println(s);
		}
	}
}

Could be compiled to a class file (named PrintFromPrompt.class), then run from the command prompt, like:
java -cp . PrintFromPrompt Hello World
and would print:

This program was run with the arguments:
Hello
World

As you can see, each word it typed in after the name of the class (the "Hello" and "World" after PrintFromPrompt) are given to the program as elements in the args array.  


Q. Why is this harder than Python?
A. There are many factors that impact programming languages, but two big ones are: 1) Different languages are used in different types of work and 2) Programming languages have histories that impact how they are designed.   Java can easily be used for some types of things, like App development, that Python isn't particularly well suited for.  Python isn't well suited for Apps because it is a little more fragile with bad data (it's a little easier to write programs that crash or fail).  Of course, Python is a little more fragile because it is a little less rigid about how variables work and that's probably one of the issues that makes Java seem harder. On the other hand, Python is very rigid about how code is indented, where as Java relies on the use of blocks (with {}) to delineate structure.  This makes it easier to mis-read Java code than Python code.   In addition, programs typically run in different ways in the two languages.  Java is a "compiled language" and Python is typically "interpreted".  This difference in how they run can also have an impact on a programmer's perception of a language. 

 

Question and Synthesis Word Cloud

Module3SynthesisWordCloud.png