copying and reversing arrays? - arrays

1.The two copies should be done in a single for loop.
2.The output should be made in a separate loop.The output should display 5 lines for each number on each one of the arrays as follows. “ARRAY1[index]= xx ARRAY2[index] = XX ARRAY3[index] = XX” Array3 should contain the first arrays numbers but reversed (5-1).
public static void main(String[] args)
{
// Constants Section
final int FIVE = 5;
final int ONE = 1;
// Variable Declaration Section
int[] firstArray = {1,2,3,4,5};
int[] secondArray;
int[] finalArray;
int i;
// Variable Initialization Section
secondArray = new int[FIVE];
finalArray = new int[FIVE];
// Code Section
for (i = 0; i <FIVE; i++)
{
secondArray = firstArray;
finalArray = firstArray;
}
for (i = FIVE - 1; i >= 0; i--)
{
System.out.println("Array1 = " + firstArray[i] + " Array2= " + secondArray[i] + " Array3= " + finalArray [i]);
}
}
}
PLEASE HELP, IM A HIGH SCHOOL STUDENT WHO IS COMPLETELY CLUELESS ABOUT PROGRAMMING. (THE SIMPLER THE BETTER)

I think this is what you need!
public static void main(String[] args)
{
// Constants Section
final int FIVE = 5;
final int ONE = 1;
// Variable Declaration Section
int[] firstArray = {1,2,3,4,5};
int[] secondArray;
int[] finalArray;
int i;
// Variable Initialization Section
secondArray = new int[FIVE];
finalArray = new int[FIVE];
// Code Section
for (i = 0; i <FIVE; i++)
{
secondArray[i] = firstArray[i];
finalArray[i] = firstArray[FIVE-i-1];
}
//For Printing you can loose any logic. This would print in the reverse order but if you want your could change the loop
for (i = FIVE - 1; i >= 0; i--)
{
System.out.println("Array1 = " + firstArray[i] + " Array2= " + secondArray[i] + " Array3= " + finalArray [i]);
}
}
}
I think this should do it, Correct me if i misunderstood..

Related

How to store point values of a triange in a point array to sort them afterwards?

I have started to do some Java exercises and now I am trying to find the longest side of a given object (i.e. triangle). In the code below, I tried to put a triangle's coordinates in a point array and try to put it on the screen to further develop the algorithm and try to sort them. Could anyone please help me understand how to use point arrays? Seems like i am missing lots of fundamental knowledge on the topic.
public void getLargestSide() {
// Put code here
FileResource fr = new FileResource();
Shape k = new Shape(fr);
Point prevPt = k.getLastPoint();
int z = getNumPoints(k);
double[] a = new double[z];
//Point currPt : k.getPoints() - For loop
for (int i= 0; i<z; i++)
{
//currPt = k.getPoints();
Point[] currPt = new Point [z];
currPt[] = k.getPoints();
double dist = prevPt.distance(currPt[i]);
a[i] = dist;
System.out.println(prevPt + "-" + currPt);
System.out.println("Distance is:" + a[i]);
System.out.println("i is:" + i);
System.out.println(" ");
System.out.println("Current Point Is:" + currPt);
System.out.println(" ");
prevPt = currPt[i];
}
//System.out.println(prevPt);
return ;
}
Hi there i finally solved it.
public double getLargestSide(Shape s) {
Point prevPt = s.getLastPoint();
int i = 0;
int z = getNumPoints(s);
Point[] a = new Point[z];
double[] b = new double[z];
double max = b[i];
for (Point currPt : s.getPoints()) {
// Find distance from prevPt point to currPt
b[i] = prevPt.distance(currPt);
// Update prevPt to be currPt
prevPt = currPt;
a[i] = currPt;
i= i + 1;
for(int j=0; j<z; j++)
{
if (b[j]>max)
{
max = b[j];
}
}
}
return max;
}

LinkedList of Arrays .. All elements get the value of the last element

I'm trying to fill a LinkedList of Arrays using For loop but the result is weird:
All elements outside my 'for' loop get the value of the last element!
However, when I do the same thing with a LinkeList of Integer this time it works all elements get its "real value". So my question is: Why it works with LinkedList of Integer but it didn't work with arrays ??
public static void main(String[] args) {
LinkedList<Integer[]> listOfArrays = new LinkedList<Integer[]>();
LinkedList<Integer> listOfInt = new LinkedList<Integer>();
Integer[] array = new Integer[2];
for(int i = 0; i <= 2; i++) {
array[0] = i;
array[1] = i+1;
listOfArrays.add(array);
listOfInt.add(i);
System.out.println("Result 1 verifying current array: " + Arrays.toString(array));
System.out.println("Result 2 verifying last added member of my list of arrays: " + Arrays.toString(listOfArrays.getLast()) + "\n");
}
System.out.println("\nResult 3 verifying the same fucking list outside 'for' loop");
for (int k = 0; k<listOfArrays.size(); k++)
System.out.println("element number " + k + ": " + Arrays.toString(listOfArrays.get(k)));
System.out.println("\nResult 4 verifying integer list outside 'for' loop");
for (int k = 0; k<listOfInt.size(); k++)
System.out.println("element number " + k + ": " + (listOfInt.get(k).toString()));
}
You are resuing the same array and overwriting its elements in each iteration of the loop. Therefore, all the elements of the list are in fact the same array, and thus they all have the data, regardless of the element you're looking at. Instead, you should create a new array in each iteration:
for (int i = 0; i <= 2; i++) {
Integer[] array = new Integer[2];
array[0] = i;
array[1] = i+1;
listOfArrays.add(array);
listOfInt.add(i);
}

Permutation of a string so that the patterns match

The question is to count how many permutations of a string B have an equivalent pattern into a bigger string A. For example, if A="aabbccd" and B="xx", then it should print 3, since "aa", "bb", "cc" are all substrings of A which share the same pattern as B.
I have tried to pass the substrings as numbers, such as xx becomes "11" and do the same for string A, but I still can't get it to work. Any ideas? Length can be up to 10^7.
Here's the code for changing pattern:
void transform(int* dest, char* original, int len) {
int j=1;
Al[original[0]-'a']=j;
dest[0]=j;
j++;
for (int i=1;i<len;i++) {
if (Al[original[i]-'a']==0)
Al[original[i]-'a']=j++;
dest[i]=Al[original[i]-'a'];
}
}
Concept: Use Regular Expressions
You would need the following regular expression (\\w)\\1{(REPETITIONS-1)}
I don't know about C but Java provides a library to compile RegEx patterns. Here's a class that implements just what you want:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringPatternPermutation {
public static void main(String[] args) {
int REPETITIONS = 3;
String REGEX = "(\\w)\\1{" + (REPETITIONS-1) + "}";
String INPUT = "abbbbbbccddeffff";
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(INPUT);
int count = 0;
while(m.find()){
String match = m.group();
System.out.println(match);
count++;
}
System.out.println(count);
}
}
Here's a test of the code above: https://ideone.com/5nztaa
Here's a useful website to test any RegEx: https://regexr.com/
Without Regular Expressions
public class StringPatternPermutation {
public static void main(String[] args) {
String a = "abjjjiixsssppw";
String b = "qwwwee";
String patternA = detectPattern(a);
String patternB = detectPattern(b);
System.out.println("-String A: " + a);
System.out.println("-Pattern A: " + patternA);
System.out.println("-String B: " + b);
System.out.println("-Pattern B: " + patternB);
System.out.println("-A contains B? " + patternA.contains(patternB));
int count = 0;
int index = 0;
while((index = patternA.indexOf(patternB)) != -1){
count++;
patternA = patternA.substring(index+1, patternA.length());
}
System.out.println("-Number of occurances: " + count);
}
private static String detectPattern(String a){
StringBuilder sb = new StringBuilder();
char prev = a.charAt(0);
int count = 1;
for(int i = 1; i < a.length(); i++){
char curr = a.charAt(i);
if(curr == prev)
count++;
else {
sb.append(count + ", ");
prev = curr;
count = 1;
}
if(i == a.length() - 1){
sb.append(count);
}
}
return sb.toString();
}
}
Test it on ideOne: https://ideone.com/w422Du

Storing each digit from double to an array

I am a newbie at this and I am not sure on how to go about to store the digits of a double to an array. I have this double value: 0120.1098
I want it to be stored as something like this:
value[1] = 0
value[2] = 1
value[3] = 2
value[4] = 0
value[5] = 1
`
and so on...
How do I do it?
I'm not sure if you really need to extract them, but as I don't know much more about your context, here you go.
You could turn it into a String, split it on . and iterate. This way you get two int arrays. One for the digits before the decimal and on for the digits after de decimal.
public static void main(String[] args) {
double d = 12323.213432;
String asString = String.valueOf(d);
String[] splitOnDecimal = asString.split("\\.");
int[] upperBound = getIntArray(splitOnDecimal[0]);
int[] lowerBound = getIntArray(splitOnDecimal[1]);
System.out.println(Arrays.toString(upperBound));
System.out.println(Arrays.toString(lowerBound));
}
private static int[] getIntArray(String numberString) {
int[] tmpArray = new int[numberString.length()];
for (int i = 0; i < numberString.length(); i++) {
tmpArray[i] = Integer.parseInt(numberString.substring(i, i + 1));
}
return tmpArray;
}
Try this, it will even show your zeroes:
String dbl = "0012300.00109800";
int dblArraylength = dbl.length();
char[] c = dbl.toCharArray();
int dotId= dbl.indexOf(".");
int a[] = new int[dblArraylength-1];
for(int i=0; i<dotId; i++){
a[i]=Integer.parseInt(Character.toString ((char)c[i]));
}
for(int i=dotId; i<a.length; i++){
a[i]=Integer.parseInt(Character.toString ((char)c[i+1]));
}
for(int i=0; i<a.length; i++){
System.out.println("a[" +i+ "] = " + a[i]);
}

How do I generate A-Z randomly to fill an array?

My assignment is to create a 3 part program which when you press 1, it runs method or "feature" 1, when you press 2, it runs feature 2, press 3, it runs feature 3. The end. I have the second feature figured out, but this feature (feature 1) is supposed to generate A-Z randomly to fill a 10x20 array. The code AS IS generates random integers and correctly fills and DISPLAYS a 10 x 20 grid. HOWEVER, how do I get it to display A-Z characters instead of numbers?
public class ModuleOnefor8 {
public static void main(String[] args) {
int[][] matrix = new int[10][20];
for (int i=0; i<matrix.length; i++) {
for (int j = 0; j<matrix[i].length; j++) {
matrix[i][j] = (int)(Math.random()*'Z'-'A' + 1);
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
Add:
public static String numberToAlpha(int number)
{
char[] ls = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
String r = "";
while(true)
{
r = ls[number % 26] + r;
if(number < 26) {
break;
}
number /= 26;
}
return r;
}
Use:
int min_int = 1;
int max_int = 9999;
string out_alpha = MyClass.numberToAlpha( Math.random() * ( max_int - min_int ) + min_int );

Resources