+ Array<E> implements a array in BRIDGES, and it can be used to create arrays of type Element<E> + + +
1D Array - An Example BRIDGES program
+Create a new .java file
+ +Imports
+-
+
- We need to include these Bridges files to give access to all the classes/methods needed to interact with Bridges +
- In your .java file, enter the following code snippets: +
+import bridges.connect.Bridges;
+import bridges.base.Array;
+import bridges.base.Element;
+
+ Main Exception
+-
+
- By adding a throw exception we can forgo messy try/catch blocks in our code for our Bridges calls +
- In your .java file, add a throw exception to the main function so it looks like this: +
public static void main(String[] args) throws Exception
+ Inside our Main
+-
+
- First we need to create our BRIDGES object and initialize our BRIDGES Credentials +
- Then we can create our Array +
- Now populate the array with dummy data. +
- Now change the color of the elements. +
- Now we pass the array object to BRIDGES +
- Finally we call the visualize function +
Bridges<String, String> bridge = new Bridges<String,String>(1, "YOUR_API_KEY", "YOUR_USER_ID");
Note that you will need to replace the two fields in this call with your BRIDGES credentials.
+ +
+//declare array size
+int arraySize = 10;
+
+//declare the array dimension
+//(10,1,1) for 1D array, where 10 is the array size
+int[] dims = {arraySize, 1, 1};
+Array<Integer> my_array = new Array<Integer> (1, dims);
+
+
+for (int k = 0; k < my_array.getSize(); k++){
+ my_array.setValue(k, new Element<Integer>(String.valueOf(k), k*k));
+ my_array.getValue(k).getVisualizer().setColor("red");
+}
+
+
+
+
+my_array.getValue(1).getVisualizer().setColor("green");
+my_array.getValue(3).getVisualizer().setColor("blue");
+my_array.getValue(5).getVisualizer().setColor("magenta");
+my_array.getValue(7).getVisualizer().setColor("cyan");
+my_array.getValue(9).getVisualizer().setColor("yellow");
+
+
+
+ bridge.setDataStructure(my_array);
+ bridge.visualize();
+ Code Summary: Your .java file should look like this
+
+
+import bridges.connect.Bridges;
+import bridges.base.Array;
+import bridges.base.Element;
+
+public class arr1d {
+public static void main(String[] args) throws Exception{
+
+ //create the Bridges object
+ Bridges<String, Integer< bridges
+ = new Bridges<String, Integer<(10, "486749122386", "kalpathi60");
+
+
+
+ // for 1D array
+ int arraySize = 10;
+ int[] dims = {arraySize, 1, 1};
+ Array<Integer> my_array = new Array<Integer> (1, dims);
+
+ for (int k = 0; k < my_array.getSize(); k++){
+ my_array.setValue(k, new Element<Integer>(String.valueOf(k), k*k));
+ my_array.getValue(k).getVisualizer().setColor("red");
+ }
+
+ my_array.getValue(1).getVisualizer().setColor("green");
+ my_array.getValue(3).getVisualizer().setColor("blue");
+ my_array.getValue(5).getVisualizer().setColor("magenta");
+ my_array.getValue(7).getVisualizer().setColor("cyan");
+ my_array.getValue(9).getVisualizer().setColor("yellow");
+
+ //set visualizer type
+ bridges.setDataStructure(my_array);
+
+ bridges.getVisualizer().setVisualizeJSON(true);
+
+ // visualize the list
+ bridges.visualize();
+ }
+}
+
+
+ Create a new .cpp file
+ +Includes
+-
+
- We need to include these Bridges headers to give access to all the classes/methods needed to interact with Bridges +
- In your .cpp file, enter the following code snippets: +
+#include "Bridges.h"
+#include "Element.h"
+
+ Namespace
+-
+
- By using this namespace we can forgo messy scope specifiers in our code for our Bridges calls +
- In your .cpp file, enter the following code snippet: +
using namespace bridges;
+ Inside our Main
+-
+
- First we need to initialize our Bridges Credentials +
- Then we can create our Array. +
- Now populate the array with dummy data. +
- Now change the color of the elements. +
- Now we pass the first element of our singly linked list to Bridges +
- Finally we call the visualize function +
Bridges::initialize(1, "YOUR_API_KEY","YOUR_USER_ID");
+ + Note that you must replace the two strings above with your BRIDGES credentials. +
+
+Element<string> *el_array = new Element<string>[10];
+
+
+
+for (int k = 0; k < 10; k++)
+ el_array[k].setLabel("Element " + to_string(k));
+
+
+el_array[0].getVisualizer()->setColor(Color("red"));
+el_array[1].getVisualizer()->setColor(Color("green"));
+el_array[3].getVisualizer()->setColor(Color("blue"));
+el_array[5].getVisualizer()->setColor("magenta");
+el_array[7].getVisualizer()->setColor("cyan");
+el_array[9].getVisualizer()->setColor("yellow");
+
+ Bridges::setDataStructure(el_array, 10);
+ Bridges::visualize();
+ Code Summary: Your .cpp file should look like this
+
+#include <iostream>
+#include <string>
+
+using namespace std;
+
+#define LOCAL_SERVER 0
+
+#include "Bridges.h"
+#include "Element.h"
+
+using namespace bridges;
+int main() {
+
+#if LOCAL_SERVER
+ Bridges<string, string> *bridges = new Bridges<string, string>(0,
+ "856807706412", "kalpathi60");
+#else
+ Bridges::initialize(0, "kalpathi60", "486749122386");
+#endif
+
+ Element<string> *el_array = new Element<string>[10];
+
+ el_array[0].setLabel("Element 0");
+
+ for (int k = 0; k < 10; k++)
+ el_array[k].setLabel("Element " + to_string(k));
+
+ el_array[0].getVisualizer()->setColor(Color("red"));
+ el_array[1].getVisualizer()->setColor(Color("green"));
+ el_array[3].getVisualizer()->setColor(Color("blue"));
+ el_array[5].getVisualizer()->setColor("magenta");
+ el_array[7].getVisualizer()->setColor("cyan");
+ el_array[9].getVisualizer()->setColor("yellow");
+
+
+ Bridges::setTitle("Array Example");
+ Bridges::setDataStructure(el_array, 10);
+ Bridges::visualize();
+
+ return 0;
+}
+
+
+ Bridges Visualization
+-
+
- Once all your code is in order, run your file. +
- Assuming all your code is correct and it compiles correctly, a +link to the Bridges website will be generated on the console. +
- Copy/paste this link into your favorite browser to view a visualization +of the data structure you just created. +
- It should look something like this: +
Well done! You’ve just created your first Bridges project!
+