Results 1 to 9 of 9
  1. #1
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default Arrays + Final = Trouble

    arrays are going to be my singlehanded downfall. I need to understand inside and out by 2weeks. Everything i can get my hands on to read about them seems brief and short but the hw problems are in depth and detail. I've tried to practice programming but thats shooting in the dark if i dont really understand them..any help would be great help..:)

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Write some code and others will no doubt explain compiler messages, strange runtime behaviour or give you a nudge in the right direction to get you started. But do make sure you review your textbook and online tutorials before you announce that you have no idea.

    Typical things you ought to be able to do with arrays include:

    * Declare and array (of both primitive and reference types)
    * Create a new array
    * Populate (=="put things into") an array and know what values where there before you populated it.
    * Use for (and other) loops to work with an array. Perennial favourites include find the minimum/maximum and average of an array of numbers.

  3. #3
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    Thank you . and i guess i bluffed a little by saying no idea. i can declare and initialize without a problem..when it comes to say like " Have user enter "x" number of intergers and count them then print to screen how many occurances.." it gets difficult...i know i would need a scanner, need variable to store results in...populating the array i know gets done with for loop... but thats where i usually get stuck when it comes to filling

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Counting the occurances is a separate problem. First how would you (what code) declare an array of ints and populate them using numbers supplied by a user?

  5. #5
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    public class Headache {
       public static void main(String[] args) {
       int[] numz = new int[51]  
    
       Scanner scan = new Scanner(System.in);
       int x = scan.nextInt();
       for (int i = 0; i < numz.length; i ++) {
             numz[i];// like i said one it gets to filling im lost but hows that..
    
       
            }
        }
    }

  6. #6
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default

    Quote Originally Posted by Bgreen7887 View Post
    ...populating the array i know gets done with for loop... but thats where i usually get stuck when it comes to filling
    Something that has helped me is to visualize what is happening with an array by writing out each array element when filling or manipulating the array. For example, let's say you have an integer array called myArray and you want to fill it with some values. This might help you visualize what is happening and to see how you might go about writing code to fill it:

    myArray[0] = some value you want to add
    myArray[1] = another value you want to add
    myArray[2] = yet another value you want to add
    myArray[3] = ... and so forth


    Here's a simple for loop that I used alot when learning to use arrays:

    Java Code:
    for(int i=0;i<myArray.length;i++) {
       System.out.println("myArray[" +i+ "]= " +myArray[i]); 
    }
    it prints the contents of myArray to the screen and would look something like:
    Java Code:
    myArray[0]= 23
    myArray[1]= 12
    myArray[2]= 43
    
    ...and so forth until the end of the array...
    Hope this helps!
    Chris
    Last edited by tashimoto; 11-23-2010 at 12:06 AM. Reason: fixed code tags

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    numz[i];// like i said one it gets to filling im lost but hows that..

    So that's a problem with Scanner as much as it is with arrays.

    Try

    Java Code:
    //int x = scan.nextInt();
    for (int i = 0; i < numz.length; i ++) {
        System.out.println("Please enter an integer");
        //numz[i];// like i said one it gets to filling im lost but hows that..
        numz[i] = scan.nextInt();
        scan.nextLine(); // need to move to the next line after reading the int
    }

    Try that (remove the commented lines so they don't litter the code). It might be a good idea to choose a size smaller than 51 for testing. And incorporate tashimoto's for loop so you can see the array values printed out again in another for loop.

  8. #8
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default Fibonacci sequence

    Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

    1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

    Find the sum of all the even-valued terms in the sequence which do not exceed four million.

    all i have sorry.need help with add previous 2 ints to produce the next..

    Java Code:
    public class SampleProblem2 
    {
    	public static void main(String[] args)
    	{
    		int[] duh = new int[100];
    		int i = 0;
    		int temp = 0;
    		
    		for(i=1;i<duh.length;i++)
    		{
    		duh[i] = i;
    		System.out.println(duh[i]);
    	
    		}
    					
    				
    
    	}
    }

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    This is a different problem and should be posted in a different thread.

    Start by thinking about how you would work out this sum. Perhaps with a lower limit (say even valued terms < 200). The chances are that, armed with pencil and paper, you won't do anything that remotely resembles using an array (the topic of this thread).

    Start a new thread and describe the steps you would use to calculate the result and, on that basis, any problems you have translating that into Java code.

    ----------------------

    Also the discussion in this thread was sort of left dangling, which isn't a way to encourage help on the new problem.

Similar Threads

  1. Arrays trouble
    By gto400no1 in forum New To Java
    Replies: 1
    Last Post: 04-14-2010, 01:20 AM
  2. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  3. HELP: Still having trouble getting arrays :(
    By Psyclone in forum New To Java
    Replies: 4
    Last Post: 02-06-2010, 01:05 AM
  4. [SOLVED] is final class members are also final ?
    By haoberoi in forum New To Java
    Replies: 4
    Last Post: 11-10-2008, 03:01 PM
  5. HELP: Trouble with partial filled arrays
    By daigre7 in forum New To Java
    Replies: 1
    Last Post: 04-07-2008, 02:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •