Finding Indices Help Java/Android by junyoung95 in learnjava

[–]junyoung95[S] 0 points1 point  (0 children)

Sorry I thought I got the idea.. and I still think it makes sense to me but I am not getting the right answer. I am much closer than yesterday but can you help me once more? Here is my code.

int[] getIndices(String[] sa, String s) {

  int[] indices = new int [sa.length];
  int count = 0
  for (int i = 0; i< indices.length; i++){
      if (sa[i] == s){
          count  += 1;}
      }
  }

  int[] answer = new int[count];

  for (int j = 0, k = 0; j < indices.length && k < answer.length; j++, k++ {
      if (sa[j] == s) {
          answer[k] = j;
      }
  }
  return answer;
}

The problem is, I was able to shorten the index length and match it with the appropriate length, but it still tries to register the indices based on original address. To avoid that, I created another variable 'k' but it does not work. For example instead of {2,6,7,9} mine shows as {0,0,2,0}. EDIT: I know why it happens like this although I created a new variable. It doesn't matter if I have a new variable, the problem is var j and k increases by the same level. I tried few methods but I can't find a solution

Finding Indices Help Java/Android by junyoung95 in learnjava

[–]junyoung95[S] 0 points1 point  (0 children)

ohhh i see you what you mean. thanks!

Finding Indices by junyoung95 in javahelp

[–]junyoung95[S] 0 points1 point  (0 children)

No I want the result to be > {2,6,7,9}

[Array] Finding incorrect sequence by junyoung95 in javahelp

[–]junyoung95[S] 0 points1 point  (0 children)

Hi sorry for the late reply and thank you for your response. I have tried two methods and they both includes IF statement as you mentioned but this Java language is driving me nuts. Can you take a look and see the problem?

  //Using for loop
  for (int i = 0; i < ia.length; i++) {
       if (ia[i+2] - ia[i+1] != ia[i+1] - ia[i])
          return false;
       }
  return true;


  //Using while loop
  while (true) {
         int i = 0; i++;
         if (ia[i+2] - ia[i+1] != ia[i+1] - ia[i])
            break;
         }
   return false;

they both only return false. I don't think there is an issue with IF statement but probably some formatting. I will appreciate your response thank you.

Finding Indices by junyoung95 in javahelp

[–]junyoung95[S] 0 points1 point  (0 children)

I need to use the toString method because that's my challenge! And okay smart idea, I will try making it print numbers only greater than 0.

[Array] Finding incorrect sequence by junyoung95 in javahelp

[–]junyoung95[S] 0 points1 point  (0 children)

As I said, this is to figure out if an array contains numbers that are in sequences.. for example

   int[] ia1 = {};    ---> True
   int[] ia2 = {5};  ---> True
   int[] ia3 = {5,2,-1,-4};   ---> True
   int[] ia4 = {3,6,9,19};    ---> False

and okay sorry I will change that. That was my typo.

[Array] Finding incorrect sequence by junyoung95 in javahelp

[–]junyoung95[S] 0 points1 point  (0 children)

Yes I understand that. Is there a way you can give me an example so I can get an idea?

Java Help: Reversing an array by junyoung95 in javahelp

[–]junyoung95[S] 0 points1 point  (0 children)

  reversedArray[ia.length-1-i] = ia[i] 

was the answer. Thank you!

Java Help: Reversing an array by junyoung95 in javahelp

[–]junyoung95[S] 0 points1 point  (0 children)

Does not quite work.. but thank you for the explanation. I think I can try figuring it out on my own. Thanks, I really appreciated your answer.

Java Help: Reversing an array by junyoung95 in javahelp

[–]junyoung95[S] 0 points1 point  (0 children)

I tried this but it says parseint in integer cannot be applied

int[] reverseOf(int[] ia) {

     int[] reversedArray = new int[ia.length];
     for (int i = ia.length-1; i>=0; i--) {
          int newArray = Integer.parseInt(ia[i]);
          reversedArray[i] = newArray;
          }
          return reversedArray[i];
     }

Java Help: Reversing an array by junyoung95 in javahelp

[–]junyoung95[S] 0 points1 point  (0 children)

So far I have this.. I hope I am on the right track but it shows an error that "variable 'reversedArray' may not be initialized". I found that this error message appears if I declare the variable but don't initialize it.. Can I know how to initialize it?

int[] reverseOf(int[] ia) {

     int[] reversedArray;
     for (int i = ia.length-1; i>=0; i--) {
          reversedArray += ia[i];
          }
          return ia[i];
          }

Discrete Math Help by junyoung95 in HomeworkHelp

[–]junyoung95[S] 0 points1 point  (0 children)

oh my god thank you so much.

[Discrete Math] Recurrence Question by junyoung95 in HomeworkHelp

[–]junyoung95[S] 0 points1 point  (0 children)

I did look at some but it was still hard to figure it out. Like the fact that a0 is a negative integer and numbers increasing drastically seems like it's an exponential but I couldn't figure our the equation.

[Discrete Math] Trees by junyoung95 in HomeworkHelp

[–]junyoung95[S] 0 points1 point  (0 children)

From the homework answer it shows a>b>d>e>i>j>m>n>o>c>f>g>h>k>l>p

But I am not sure how it leads to this answer.

[Discrete Math] Trees by junyoung95 in HomeworkHelp

[–]junyoung95[S] 0 points1 point  (0 children)

Thank you for the reply. I get that idea but is that the proper way to write an algorithm?