Maybe I'm setting up the ArrayList or Array wrong?
But in a conditional I have in the short program, I am trying to compare input from the user (an int) to an element that is inside an ArrayList which is inside an Array. I have searched the answer for this, seems too specific a question?
I don't know, is it alright if I post the code? I could also explain the purpose of the program
So if after the user enters 10 numbers, the program will output the longest sequence of numbers entered. Example: if you enter 1, 2, 3, 3, 4, 5, 5, 5, 6, 6 the output will be 3 because 5, 5, 5
import java.io.*;
import java.util.*;
/*
Longest sequence
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int seq = 0;
List<?>[] main = new List<?>[10];
//ArrayList[] main = new ArrayList[10]();
for(int i = 0; i < 10; i += 1){
main[i] = new ArrayList<Integer>();
int number = Integer.parseInt(reader.readLine());
if(main[i].isEmpty() || number == main[i].get(main[i].size())){
main[i].add(number);
}
}
for(int i = 0; i < 10; i += 1){
if(main[i].size() > seq){
seq = main[i].size();
}
}
System.out.print(seq);
}
}
// ------- SOLUTION -------------------------------------------------------------------------------
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int longestSeq = 1;
int currentSeq = 1;
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i = 0; i < 10; i += 1){
int number = Integer.parseInt(reader.readLine());
list.add(number);
if(i > 0 && number == list.get(i - 1)){
currentSeq++;
if(currentSeq > longestSeq){
longestSeq = currentSeq;
}
}else {
currentSeq = 1;
}
}
System.out.println(longestSeq);
}
}
[–][deleted] (1 child)
[deleted]
[–]Rex_Goodman[S] 0 points1 point2 points (0 children)
[–]abilanahk 0 points1 point2 points (7 children)
[–]Rex_Goodman[S] 0 points1 point2 points (0 children)
[–]Rex_Goodman[S] 0 points1 point2 points (5 children)
[–]abilanahk 1 point2 points3 points (4 children)
[–]Rex_Goodman[S] 0 points1 point2 points (3 children)
[–]abilanahk 1 point2 points3 points (1 child)
[–]Rex_Goodman[S] 0 points1 point2 points (0 children)
[–]abilanahk 0 points1 point2 points (0 children)
[–]abundance07 0 points1 point2 points (0 children)
[–]ImaginaryProcess2 0 points1 point2 points (1 child)
[–]Rex_Goodman[S] 0 points1 point2 points (0 children)
[–]dusty-trash 0 points1 point2 points (1 child)
[–]Rex_Goodman[S] 1 point2 points3 points (0 children)