Results 1 to 11 of 11
- 04-03-2009, 11:27 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 4
- Rep Power
- 0
- 04-03-2009, 11:53 PM #2
Member
- Join Date
- Mar 2009
- Posts
- 40
- Rep Power
- 0
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]); } } }
- 04-04-2009, 01:04 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 4
- Rep Power
- 0
Im looking for programs that ask the user to enter data into arrays.
- 04-04-2009, 01:13 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
!
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.
- 04-04-2009, 01:21 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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]); } }
- 04-04-2009, 01:35 AM #6
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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.
- 04-04-2009, 02:24 AM #7
Member
- Join Date
- Apr 2009
- Posts
- 4
- Rep Power
- 0
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");
}
- 04-04-2009, 03:11 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
No - main() is not in the slightest bit special. Whatever you can do in main you can do in other methods.I guess I'm confused about where I can create an array. Can I only create it in the main()?
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.
- 04-04-2009, 08:37 AM #9
Member
- Join Date
- Mar 2009
- Posts
- 40
- Rep Power
- 0
- 04-04-2009, 04:08 PM #10
Parrallel arrays (have to be two or more) are those arrays that have related elements between them at the same element index. For example...
This could be represented by two arrays the following way:Sam lives in Los Angeles, Bob lives in New York and Larry lives in 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.Java Code:nameArray = {Sam, Bob, Larry}; cityArray = {Los Angeles, New York, Houston};
Something like that....
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-04-2009, 04:11 PM #11
Member
- Join Date
- Mar 2009
- Posts
- 40
- Rep Power
- 0
Similar Threads
-
[SOLVED] Parallel Arrays with Choice ComboBox - need assistance
By Judoon_Platoon in forum Java AppletsReplies: 14Last Post: 10-01-2008, 09:07 PM -
Dialog Examples in SWT
By Java Tip in forum SWTReplies: 0Last Post: 07-02-2008, 07:54 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks