Home java Add values ​​to List & lt; String & gt;

Add values ​​to List & lt; String & gt;

Author

Date

Category

String – as I understand in this context, this is a class that inherits from the Object.

But if I need to add parameters there, or I don’t know how to correctly say variables chtoli, then how can I do it?

Found several solutions:

ArrayList & lt; String & gt; list = new ArrayList & lt; String & gt; ();
list.add (textview.getText (). toString ());
list.add ("B");
list.add ("C");

But this does not quite suit me. Can you clarify this cryptic class a little?

For example, I need to add photos to be added from the gallery to the list.


Answer 1, authority 100%

String – as I understand in this context, this is a class that inherits from the Object.

Colleague, to understand correctly, follow a simple algorithm:

  1. Write your code in the IDE.
  2. Hold down ctrl and click on an incomprehensible word.

In your case, String is the same string. That is, your

List & lt; String & gt; allPhoto = new ArrayList & lt; & gt; ();

Will contain strings, not photos.
It’s just a collection, if you like – a dynamic array, that is, it changes its length when new elements are introduced. As the author told you above, the type of object that will be an element of the array is shown in angle brackets.

As I understand it, you have a certain class of photo that has some fields. For example, length and width. Let it be implemented something like this (quite simplified):

public class Photo {
 int width; //width
 int height; //height
 public int getWidth () {// length getter
return width;
}
public int getHeight () {// height getter
  return height;
}
public Photo (int width, int height) {// constructor
  this.width = width;
  this.height = height;
}
}

Then your list will look like this:

List & lt; Photo & gt; allPhoto = new ArrayList & lt; & gt; ();

Let you add a photo class object. Do it like this:

allPhoto.add (0, new Photo (800, 600)); // add a photo 800 * 600 to the zero element
   allPhoto.add (1, new Photo (640, 480)); // add a 640 * 480 photo to the first element
   allPhoto.add (2, new Photo (1280, 800)); // add a photo 1280 * 800 to the second element

If you have, as you say, a “gallery” then in place of new Photo there should be a command like addFromGallery which returns an object of class Photo .

Suppose your boss told you:

there is a collection allPhoto. Get me the width and height of the second
photos.

You do it like this:

Photo ph2 = allPhoto.get (2); // got the second object from the collection;
  int Widthph1 = ph1.getWidth (); // got the width from the second photo
  int Heightph1 = ph1.getHeight (); // got the height from the second photo

Answer 2, authority 75%

When you create a new object for an array, you immediately indicate in angle brackets which type you are going to use.


Answer 3, authority 50%

If I understand the question correctly, then you need to store in the list not just a list of String, but a String complete with some additional parameters.
This can be achieved by creating a data structure that suits your needs and placing it in the list. For example:

class PhotoWithParams {
  String photo;
  String paramA;
  String paramB;
  public PhotoWithParams (String photo, String paramA, String paramB) {
   this.photo = photo;
   this.paramA = paramA;
   this.paramB = paramB;
  }
  // further getters for parameters ...
  // public String getPhoto () ... etc.
}

Next is your code:

ArrayList & lt; PhotoWithParams & gt; list = new ArrayList & lt; PhotoWithParams & gt; ();
list.add (new PhotoWithParams (textview.getText (). toString (), "B", "C"));

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions