8 Replies - 687 Views - Last Post: 15 November 2012 - 05:19 PM Rate Topic: -----

#1 bruins301  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 15-November 12

General Understanding of Arrays

Posted 15 November 2012 - 01:06 PM

I hear a lot about stack overflows and stacks in coding. I'm just curious as to how these concepts work when looking into arrays. Also what would the easiest way be for loading in a file of integers into an array. I've gone about it like so...

public static double initializeArr (Scanner kbd) {
int size = kbd.nextInt();
double value[] = new Double[size];
for (int i = 0; i < value.length ; i++) {
value[i] = kbd.nextDouble();
}
return value;

This post has been edited by jon.kiparsky: 15 November 2012 - 01:08 PM
Reason for edit:: please use code tags and format your code properly!


Is This A Good Question/Topic? 0
  • +

Replies To: General Understanding of Arrays

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7575
  • View blog
  • Posts: 12,725
  • Joined: 19-March 11

Re: General Understanding of Arrays

Posted 15 November 2012 - 01:19 PM

Stack overflow and stacks as data structures are pretty widely separated. Stack overflow: many languages represent memory as a stack. When you call a function or a method, you place it on the stack, and follow the instructions until you get to a return. Then you pop it off the stack, leaving the return value (if any) where the calling method can find it.

Stack overflow occurs when you push more functions onto the stack than you have memory to cope with. This is usually due to recursion gone wild or some similar coding error.

Stacks, speaking more generally, are a core data structure. The stack property is simply FILO: First In, Last Out. Take a deck of cards and deal them out one at a time, face up, then take one off the top. It's the last one you dealt. Repeat the remove (pop) operation until you're holding all the cards again: the last one you picked up is the first one you dealt. (you just reversed your stack). This is easiest to implement on a linked list, but you can implement it on an array - that's a little bit complicated. See Robert Sedgewick's seminal textbook on Algorithms to learn more. It's pricey, but it's the best volume on algorithms and data structures for the student, and the most recent release is all Java.

So that's stacks.


Your method has a basic problem in that it declares a double as the return type, but you actually return an array of doubles. That's easy to fix. The rest of the method looks okay, I guess.
Was This Post Helpful? 1
  • +
  • -

#3 bruins301  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 15-November 12

Re: General Understanding of Arrays

Posted 15 November 2012 - 01:27 PM

Thank you for your input. I have another question regarding changing the values of an array. This is the question.
Given
*an int variable k ,
*an int array incompletes that has been declared and initialized,
*an int variable studentID that has been initialized, and
*an int variable numberOfIncompletes ,
write code that counts the number of times the value of studentID appears in incompletes and assigns this value to numberOfIncompletes .

You may use only k , incompletes , studentID , and numberOfIncompletes .
This is my code, but I know its off by a lot.
numberOfIncompletes = 0;
if(studentID == incompletes[]) {
for (k = 0; k < incompletes.length; k++) {
numberOfIncompletes += incompletes[k];
}
}

This post has been edited by jon.kiparsky: 15 November 2012 - 01:40 PM

Was This Post Helpful? 0
  • +
  • -

#4 bruins301  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 15-November 12

Re: General Understanding of Arrays

Posted 15 November 2012 - 01:38 PM

numberOfIncompletes = 0;
if(studentID == incompletes[]) {
for (k = 0; k < incompletes.length; k++) {
numberOfIncompletes += incompletes[k];
}
}


Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7575
  • View blog
  • Posts: 12,725
  • Joined: 19-March 11

Re: General Understanding of Arrays

Posted 15 November 2012 - 01:44 PM

As always, think about how you'd do this if you were the computer.

Here's your incompletes array:

234532
654673
234564
234577
634563
654673

Here's StudentID:

654673

And numberOfIncompletes is for you to calculate. You may use k as an index variable.

What do you do?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: General Understanding of Arrays

Posted 15 November 2012 - 02:17 PM

also
double value[] = new Double[size];
should be
double value[] = new double[size];
Was This Post Helpful? 2
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7575
  • View blog
  • Posts: 12,725
  • Joined: 19-March 11

Re: General Understanding of Arrays

Posted 15 November 2012 - 02:24 PM

View Postpbl, on 15 November 2012 - 04:17 PM, said:

also
double value[] = new Double[size];
should be
double value[] = new double[size];



Good catch!
Was This Post Helpful? 0
  • +
  • -

#8 bruins301  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 15-November 12

Re: General Understanding of Arrays

Posted 15 November 2012 - 05:04 PM

I think I found the solution to my other problem.
numberOfIncompletes = 0;
for(k = 0; k < incompletes.length; k++) {
if(studentID == incompletes[k]) {
numberOfIncompletes ++ ;
}
}


The code compiled fine. I keep thinking of arrays as something different I should just look at them as a collection of variables. BTW good catch with the error in spelling I would not have caught that.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7575
  • View blog
  • Posts: 12,725
  • Joined: 19-March 11

Re: General Understanding of Arrays

Posted 15 November 2012 - 05:19 PM

View Postbruins301, on 15 November 2012 - 07:04 PM, said:

I keep thinking of arrays as something different I should just look at them as a collection of variables.


Yes and no. Yes, each element of an array is a location in memory pointed to by a name, and anything you can do with a named variable you can do with an array element (pass it as a value to a method, perform arithmetic on it, call methods on it, whatever). However, no, in that there are things you can do with arrays that you can't do with a handful of named variables. (iterate over them, sort them, return them from a method, etc)

Also, semantically speaking, an array provides a layer of abstraction: now you can think of these things as a collection, not just as a bunch of things. This communicates some useful information to whoever is going to work on this code (which might be you, later on) Getting used to that capacity for abstraction will help you a great deal when you move on to other collections and other data structures.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1