Results 1 to 11 of 11
  1. #1
    dangerzone9k is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default I need examples using parallel arrays

    Hello,

    I am getting confused using arrays and I believe if I see examples of parallel array code it will make more sense. The code does not need to be complex.

    Thanks

  2. #2
    Natrix is offline Member
    Join Date
    Mar 2009
    Posts
    40
    Rep Power
    0

    Default

    Heres something we had for one of our assignments. Creates an array (table) of 15 integers. If the number is dividable by 2 it increases the number for 1 otherwises does nothing to it. It then prints out the numbers.

    Java Code:
    /**
     * 
     * 
     * @author (Natrix) 
     * @version (25.11.2008)
     */
    
    
    public class Ass5
    {
    
    
    
        public static void main(String[] arg)
        {
            int t[]= new int[15];
                for (int i = 0; i <15;i++) {
                    if (i % 2 == 0) 
                    t[i]++;
                System.out.println(t[i]);
            }
        }
    }

  3. #3
    dangerzone9k is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    Im looking for programs that ask the user to enter data into arrays.

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

    Default

    Quote Originally Posted by dangerzone9k View Post
    I believe if I see examples of parallel array code it will make more sense
    You see lots of these parallel arrays - usually as a result of badly posed assignment questions. Personally the more I see the less and less sense they make. Use a class whose fields represent the various characteristics recorded by the arrays.

    Quote Originally Posted by dangerzone9k View Post
    Im looking for programs that ask the user to enter data into arrays.
    !

    Seriously, how about consulting your textbook or notes and writing such an example for yourself? If it fails to compile or does not do what you expect, then post what you've got and a description of the problem. At least that way you'll have a specific question to ask.

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

    Default

    Quote Originally Posted by Natrix View Post
    Heres something we had for one of our assignments. Creates an array (table) of 15 integers.
    This isn't really an example of parallel arrays. It could be made into one, though:

    Java Code:
    public class Ass5b
    {
        public static void main(String[] arg)
        {
            int[] val= new int[15];
            boolean[] isEven = new boolean[15];
    
            for (int i = 0; i < 15; i++) 
            {
                val[i] = i;
                isEven[i] = i % 2 == 0;
            }
    
            System.out.println("val[7]=" + val[7] + ", isEven[7]=" + isEven[7]);
        }
    }

  6. #6
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    like pbrockway2 said, parallel arrays are counterintuitive to classes/oop (and probably all of programming. even in c, structs can combine multiple variables). i don't see what you don't understand about parallel arrays, though. if you can deal with one, you can deal with multiple ones.

  7. #7
    dangerzone9k is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    I guess I'm confused about where I can create an array. Can I only create it in the main()?


    import javax.swing;
    public static viod main(String[] args)
    {
    String[] names = new String[10];
    enter (names);
    }
    private static void enter(String [] names)
    {
    for(int i=0; i < 10; i++)
    names[i] = JOptionPane.showInputDialog("Enter names");

    }

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

    Default

    I guess I'm confused about where I can create an array. Can I only create it in the main()?
    No - main() is not in the slightest bit special. Whatever you can do in main you can do in other methods.

    The thing about the main() method is that it can be started up "automatically" when your program runs. So if you decide to create the array in some other method that method will have to be called from main(). (directly or indirectly).

    That code you posted is fine and would populate the names[] array ... provided you fix the mistakes in it. Post compiler messages if you can't understand them.

  9. #9
    Natrix is offline Member
    Join Date
    Mar 2009
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    This isn't really an example of parallel arrays. It could be made into one, though:

    Java Code:
    public class Ass5b
    {
        public static void main(String[] arg)
        {
            int[] val= new int[15];
            boolean[] isEven = new boolean[15];
    
            for (int i = 0; i < 15; i++) 
            {
                val[i] = i;
                isEven[i] = i % 2 == 0;
            }
    
            System.out.println("val[7]=" + val[7] + ", isEven[7]=" + isEven[7]);
        }
    }
    Actualy I didn't even know what a parallel array is. I googled it and it said that its data structure with multiple data of same type.

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Parrallel arrays (have to be two or more) are those arrays that have related elements between them at the same element index. For example...
    Sam lives in Los Angeles, Bob lives in New York and Larry lives in Houston.
    This could be represented by two arrays the following way:
    Java Code:
    nameArray = {Sam, Bob, Larry};
    cityArray = {Los Angeles, New York, Houston};
    If I want to know the city where Sam lives, I find out what he's index is in nameArray and use that index to find the city in cityArray.

    Something like that....

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    Natrix is offline Member
    Join Date
    Mar 2009
    Posts
    40
    Rep Power
    0

    Default

    Thank you, this is a very good explanation.

    It is sometimes difficult to understand expressions that are used in programming because we learn in our language (Slovenian) in school.

Similar Threads

  1. [SOLVED] Parallel Arrays with Choice ComboBox - need assistance
    By Judoon_Platoon in forum Java Applets
    Replies: 14
    Last Post: 10-01-2008, 09:07 PM
  2. Dialog Examples in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 07:54 PM

Posting Permissions

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