[PDF] [PDF] 2D Array Problems - Rose-Hulman

This code prints out a two dimensional array of integers The function returns the distance between the of the pair in the map of java they have to be typed



Previous PDF Next PDF





[PDF] Two-Dimensional Arrays

Two-dimensional (2D) arrays are indexed by two subscripts, one for the row and one for the column Each element in the 2D array must by the same type, either a primitive type or object type



[PDF] Chapter 7 Multidimensional Arrays

Listing 7 1 gives an example with a method that returns the sum of all the elements in a matrix LISTING 7 1 PassTwoDimensionalArray java (Page 240) import 



[PDF] Multidimensional Arrays

In Java, each subscript must be enclosed in a pair of square brackets • You can also use 8 4 Passing Two-Dimensional Arrays to Methods • You can pass a 



[PDF] CS 106A, Lecture 17 2D Arrays and Images

Java infers the array length The class Arrays in package java util has useful methods for A 2D array is an array where every element is itself an array



[PDF] 2D Array Exercise

Practice using two dimensional arrays to input and output data in a tabular format A Simple 2D TwoDArray java for (int col=0; col < table[row] length; col++)



[PDF] Topic 26 Two Dimensional Arrays - UT Austin Computer Science

Jeffery Ullman Based on slides for Building Java Programs by Reges/Stepp, found at Write a method to find the max value in a 2d array of ints Write a 



[PDF] Unit 8: 2D Arrays - Long Nguyen

1) Building Java Programs: A Back to Basics Approach by Stuart Reges 2D Arrays A two-dimensional (2D) array has rows and columns A row has horizontal elements 2) For each k, where 0



[PDF] 2D Array Problems - Rose-Hulman

This code prints out a two dimensional array of integers The function returns the distance between the of the pair in the map of java they have to be typed



[PDF] Using Two-Dimensional Arrays

The new, five-floor Java Hotel features a free continental breakfast and, at The people who do serious Java like to think of a two-dimensional array as an array of with an array of objects, you're taking advantage of methods that are already

[PDF] 2d dft example

[PDF] 2d dft in digital image processing

[PDF] 2d discrete fourier transform matlab code

[PDF] 2d fft algorithm

[PDF] 2d fft image matlab

[PDF] 2d fft of image

[PDF] 2d fft radar

[PDF] 2d fft symmetry

[PDF] 2d fftshift

[PDF] 2d fourier transform examples

[PDF] 2d fourier transform mathematica

[PDF] 2d fourier transform properties

[PDF] 2d heat equation derivation

[PDF] 2d image to 3d model python

[PDF] 2d picture to 3d model

2D Array Problems

This code prints out a two dimensional array of integers. public static void printOutArray(int[][] array) { for(int i = 0; i < array.length; i++) { for(int j = 0; j < array[i].length; j++) {

System.out.printf("%3d ",array[i][j]);

System.out.println();

This code constructs an array containing multiplication table for the given starting and ending integers.

public static int[][] createMultiplicationTables(int startingAt, int goingTo) { int sizeOfRange = goingTo - startingAt + 1; int[][] result = new int[sizeOfRange][sizeOfRange]; for(int i = 0; i < sizeOfRange; i++) { for(int j = 0; j < sizeOfRange; j++) { result[i][j] = (startingAt + i)*(startingAt+j); return result;

This code takes an array representing a map of positions and an element to search for. The searched for

HOHPHQP RLOO H[LVP LQ POH MUUM\ H[MŃPO\ 2 PLPHV POLV LV POH ߡSMLUߢ

The function returns the distance between the of the pair in the map of positions. The distance is the Manhattan

distance (i.e. the distance in terms of number of steps directly north, south, east, or west). So for example, given

the array: aa.b ..b. findPairDistance(map,'a') yields 1 (because is right next to the other a) findPairDistance(map,'b') yields 3 (because it takes 3 steps to get from the first b to the second = west south south) public static int findPairDistance(char[][] map, char pairToFind) { int NOT_FOUND = -1; int firstRow = NOT_FOUND; int firstColumn = NOT_FOUND; for(int i = 0; i < map.length; i++) { for(int j = 0; j < map[i].length; j++) { char current = map[i][j]; if(current == pairToFind) { if(firstRow == NOT_FOUND) { firstRow = i; firstColumn = j; } else { int distance = Math.abs(firstRow - i) +

Math.abs(firstColumn - j);

return distance; //should never get here return NOT_FOUND;

Map Problems

Maps are pretty much the same as Dictionaries in Python. This function constructs a map associating a number with it's largest divisor. So for example, numberToLargestDivisior(10) yields {2=1, 3=1, 4=2, 5=1, 6=3, 7=1, 8=4, 9=3, 10=5} public static HashMap numberToLargestDivisor(int maxNum) { HashMap map = new HashMap(); for(int i = 2; i <= maxNum; i++) { int numberToFindDivisorOf = i; for(int j = numberToFindDivisorOf / 2; j > 0; j--) { if(numberToFindDivisorOf % j == 0) { map.put(numberToFindDivisorOf, j); break; return map;

This function takes a map and returns a new map of where both the keys and values are double. So far

example, {A=a, BB=bb} yields {AA=aa, BBBB=bbbb} public static HashMap doubleMap(HashMap originalMap) { HashMap result = new HashMap(); for(String key : originalMap.keySet()) { result.put(key + key, originalMap.get(key) + originalMap.get(key)); return result;

This function takes an array of strings and returns the most frequent beginning letter. So for example, the strings

public static char findMostFrequentStartingLetter(String[] strings) { HashMap letterToFreq = new HashMap(); for(String current : strings) { char startingChar = current.charAt(0); if(!letterToFreq.containsKey(startingChar)) { letterToFreq.put(startingChar, 0); int currentFrequency = letterToFreq.get(startingChar) + 1; letterToFreq.put(startingChar, currentFrequency); //ok now find the most frequent int highestFrequency = 0; char charWithHighest = '\0'; for(char current : letterToFreq.keySet()) { if(letterToFreq.get(current) > highestFrequency) { highestFreq = letterToFreq.get(current); charWithHighest = current; return charWithHighest;

Array Intro

//hint ߙ // A 2D array in Java stores a whole table of information // It has 2 indexes - a row and column // You can make 2D arrays of any shape, similar to the way you // make 1D arrays. int[][] myIntArray = new int[50][7]; //50x7 array String[][] myStringArray = new String[10][200]; //10x200 array // Set and get elements like you expect myIntArray[45][5] = 77; myStringArray[6][157] = "hello"; System.out.println("value at index 45 5 " + myIntArray[45][5]); // note that both indexes start at 0 System.out.println("largest indexes in 2d string array " + myStringArray[9][199]); // if you want to get the dimensions, the usual length will give you the first // dimension System.out.println(myIntArray.length); //prints 50 //to get the second dimension, do it like this System.out.println(myIntArray[0].length); //prints 7 // actually you could use any valid index instead of 0, but they'll // all give you the same length

Map Intro

//hint ߙ // A map is like a dictionary in Python // It associates a key with a particular value - but because this is // java they have to be typed // Format: HashMap foo = new HashMap(); // e.g. HashMap namesToWeight = new HashMap(); //to add elements to the map, use put namesToWeight.put("Buffalo", 160); namesToWeight.put("Gretchen", 130); //note that putting twice with the same key overwrites the value namesToWeight.put("Buffalo", 165); //to get elements out of the map, use get System.out.println("Buffalo's weight is " + namesToWeight.get("Buffalo")); //if you need to check if a particular key is in the map, use containsKey if(namesToWeight.containsKey("Steve")) {

System.out.println("Steve is in the map!");

//if you need iterate over all the keys in the map, use the keyset

Set keys = namesToWeight.keySet();

//it's annoying to iterate over a set. Use the enhanced for loop: for(String key : keys) { int value = namesToWeight.get(key); //do something for every key and valuequotesdbs_dbs4.pdfusesText_7