I have some question about the code below. It works correctly, but:
- Is there a better/another way to solve the exercise (as an expert would have done:) or mine it's perfectly done?
- Also, I don't see the point of using ArrayList instead of a normal vector like
int a[]
. - In the method
analysis
I wanted to set as parameters(ArrayList a,ArrayList b)
but an error (incompatible types List cannot be converted toArrayList
) appears in thisint a = Testing.analysis(seq1, seq2, t1);
line. I thought I was working withArrayList
since I instantiated my sequences as such.
Exercise:
Perform a program that uses a function that returns a 1 if two very large numbers (digits greater than 5 and where each digit is an element of the arrayList) are equal and a 0 otherwise.
Example 1:
Entry
Number 1: 2 3 4 5 6 7
Number 2: 1 4 6 8 9 0
Output "They are not equal numbers"
class Testing { static int analysis(List a, List b, int t) throws Exception { //using //ArrayList instead of List marks an error, why? int c = 0; if (a.size() == b.size() && a.size() > 5) { for (int i = 0; i < t; i++) { if (a.get(i) == b.get(i)) { c++; } } } else { System.out.println("oops! the lengths are not the same or some of the sequence is less than 6"); throw new Exception(); //to finish the method } if (c == t) { return 1; } else { return 0; } } } public class Tarea24agoENGLISH { public static void main(String[] args) throws Exception { Scanner q = new Scanner(System.in); List seq1 = new ArrayList(); //I wanted to write this List <int> seq=new ArrayList<int>(); but it marked an error, why? System.out.println("Introduce the length of the sequence1 "); int t1 = q.nextInt(); for (int i = 0; i < t1; i++) { System.out.println("Introduce the digits for sequence1"); int n = q.nextInt(); seq1.add(n); } List seq2 = new ArrayList(); System.out.println("Introduce the length of the sequence2"); int t2 = q.nextInt(); for (int i = 0; i < t2; i++) { System.out.println("Introdce the digits for sequence2"); int n = q.nextInt(); seq2.add(n); } int a = Testing.analysis(seq1, seq2, t1); if (a == 1) { System.out.println("same numbers"); } else { System.out.println("\n different numbers"); } } }
int[]
is not a vector, it is an array. There is no such thing as a vector in Java, except for an old, outdated class calledjava.util.Vector
that should never be used.\$\endgroup\$