Solved

Is there a way to cycle through array positions based on flags?

Posted on 2002-04-02
6
198 Views
Last Modified: 2010-03-31
I'm printing comments from an array and the input constantly varies. I'm cycling through an array, printing comments but in this case there are about 8 different conditions. In some conditions some comments should be left out (based on already flags I set).
Does java allow you to jump positions in an array (inside a loop) based on conditions?

Here's a crude example of what I need to happen.

String comment[] new String[14]
  comment[1]="AAAAA";
  comment[2]="BBBBB";
  comment[3]="CCCCC";
  comment[4]="DDDDD";
  ...
  comment[14]="NNNNN";

if (condition 1 == some number) {
  //print comments [1],[3],[7]
}else if (condition 1 == other number) {
  //print comments [1]...[5],[8],etc.
}

The comment positions are just examples. I just want to know how (if possible) to achieve this type of condition based cycling through the array.
0
Comment
Question by:webratss
6 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 100 total points
ID: 6913454
if i understand you correctly, then yes:

if (condition 1 == some number) {
   System.out.println(comments[1] + " " comments[3] + " " comments[7]);
}else if (condition 1 == other number) {
   System.out.println(comments[1] + " " comments[5] + " " + etc...);
}


0
 

Expert Comment

by:rashidkamranbs
ID: 6913585
Well webratss you are confused if random access ot array is allowed.. there is no doubt you can .. access any element at any time.. just inex should be with in the range of an array size..

this way.. you can print any string from ur Array.. no matter whats ur previous index were or whats ur next message would be..

0
 

Expert Comment

by:0xDEADBEEF
ID: 6915281
Hmmm... I would try something like this:

String comment[] = { "AAAA", "BBBB", "CCCC", /*...*/ "NNNN" };
int indices[];

switch(condition){
  case some_number:
    indices = new int[] {1, 3, 7};
    break;
  case other_number:
    indices = new int[] {1, 2, 3, 4, 5, 8};
    break; //and so on. don't forget
  default:
    indices = new int {}; //so in case nothing applies, nothing will be printed.
}

for(int i = 0; i < indices.length; i++){
  System.out.println(comment[indices[i]]);
}

This is easier to maintain, afaic. Hope this helps.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:triso
ID: 6932211

Also a note for webratsass,

array indices go from 0 to n-1 so your example should be:

String comment[] new String[14]
 comment[0]="AAAAA";
 comment[1]="BBBBB";
 comment[2]="CCCCC";
 comment[3]="DDDDD";
 ...
 comment[13]="NNNNN";


Hope this helps.
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7932544
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: bobbit31
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
0
 

Expert Comment

by:SpideyMod
ID: 7984159
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 22
topping1 challenge 7 73
Which is the latest version of Apache Tomcatl 3 50
Java SE 8u111  Lot of stuff broke 11 54
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now